3
我嘗試將輻射適用於形UIView
的UIView與CAShapeLayer不顯示CAGradientLayer
的gradiant類我使用,如果經過充分測試,並在我的代碼另一部分使用了自定義。 但是,我不能讓它在自定義形狀視圖正常使用CAShapeLayer
建立在我UIView類:
@implementation UIView (Grandiant)
- (void)addGradientBackgroundFromTopColor:(UIColor *)topColor toBottomColor:(UIColor *)bottomColor {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.frame;
gradient.colors = [NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil];
[self.layer insertSublayer:gradient atIndex:0];
}
@end
在我UIView子類:
override func awakeFromNib() {
super.awakeFromNib()
self.addGradientBackgroundFromTopColor(UIColor(red: 221.0/256.0, green: 0.5, blue: 32.0/256.0, alpha: 1), toBottomColor: UIColor(red: 220.0/256.0, green: 94.0/256.0, blue: 17.0/256.0, alpha: 1))
}
override var frame: CGRect {
didSet {
let bezierpath = UIBezierPath()
bezierpath.moveToPoint(CGPoint(x: 0, y: 0)) // 1
bezierpath.addLineToPoint(CGPoint(x: 0, y: frame.height - triangleHeight)) // 2
bezierpath.addLineToPoint(CGPoint(x: frame.width/2, y: frame.height))// 3
bezierpath.addLineToPoint(CGPoint(x: frame.width, y: frame.height - triangleHeight))// 4
bezierpath.addLineToPoint(CGPoint(x: frame.width, y: 0))// 5
let mask = CAShapeLayer()
mask.frame = bounds
mask.path = bezierpath.CGPath
layer.mask = mask
無論backgroundColor分配給該視圖,視圖都會顯示。 我嘗試在應用蒙版後設置漸變,沒有太多結果。
它的工作原理,謝謝:) – Antzi