在CAShapeLayer
上,我繪製了一個閉合的UIBezierPath
。我可以通過設置fillColor
來填充這個形狀,但是我想用漸變填充形狀。我如何設置CAGradientLayer
,以便剪切到bezier路徑概述的形狀?在iOS上用漸變填充路徑
8
A
回答
5
你想要的是一個面具。 CAGradientlayer有-setMask方法,可以把它夾到你的形狀的界限,像這樣:
[gradientLayer setMask:shapeLayer];
19
一個例子草案將是以下幾點:
...
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *gradientColor = [UIColor colorWithRed:0.51 green:0.0 blue:0.49 alpha:1.0];
NSArray *gradientColors = [NSArray arrayWithObjects:
(id)[UIColor blueColor].CGColor,
(id)gradientColor.CGColor,
(id)[UIColor redColor].CGColor, nil];
CGFloat gradientLocations[] = {0, 0.5, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)gradientColors, gradientLocations);
UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 200, 200) cornerRadius:6];
CGContextSaveGState(context);
[roundedRectanglePath fill];
[roundedRectanglePath addClip];
CGContextDrawLinearGradient(context, gradient, CGPointMake(10, 10), CGPointMake(210, 10), 0);
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
...
相關問題
- 1. 如何用漸變色填充貝塞爾路徑
- 2. 如何用drawRect中的漸變填充路徑:?
- 3. 用漸變填充geom_tile
- 4. 在iOS上,如何用大綱填充路徑? (或兩個填充和描邊路徑)
- 5. RaphaelJS路徑對象圖像填充和漸變在CodeIgniter中不起作用?
- 6. 沿路徑漸變
- 7. WPF在MouseOver上更改路徑填充
- 8. 漸變不填充IBDesignable UIView
- 9. 矩形漸變填充
- 10. iOS。 OpenGL的。用漸變填充曲線下面積線
- 11. Raphael.js路徑/行與漸變?
- 12. SVG使用和漸變作爲填充
- 13. ggplot漸變填充不起作用
- 14. 使用jQuery進行漸變填充?
- 15. vb6:用漸變填充多邊形
- 16. 如何用傾斜漸變填充CAShapeLayer?
- 17. 動畫填充路徑 - Silverlight
- 18. 將鼠標輸入的填充路徑用作填充路徑之間的填充?
- 19. 有沒有辦法在對角線上填充線性漸變?
- 20. OpenGL的漸變填充在iPhone上看起來條紋
- 21. 漸變填充和畫布大小?
- 22. 漸變色填充拉斐爾
- 23. NSWindow紋理漸變填充怪異
- 24. 基於列值的漸變填充
- 25. 具有漸變填充的NSView?
- 26. 漸變填充react-highcharts的折線圖
- 27. 柔性圖表漸變填充
- 28. WPF圖表刪除漸變填充
- 29. 如何創建UIBezierPath漸變填充?
- 30. 三向色彩漸變填充r
這是很好的,但你也應該釋放漸變和顏色空間。 – Sulthan 2013-03-17 13:48:52
感謝,我忘了把它們 – WhiteTiger 2013-03-17 14:23:38
,因爲原帖特色面具,我還增加了以下內容: 'CGContextAddPath(背景下,roundedRectanglePath.CGPath);'' CGContextClip(背景);' CGContextDrawLinearGradient'之前' – maggix 2013-07-04 10:32:43