带有 CAShapeLayer 的 UIView 不显示 CAGradientLayer
UIView with CAShapeLayer does not display CAGradientLayer
我尝试将辐射应用于自定义形状 UIView
如果在我的代码的其他部分经过良好测试和使用,我使用的渐变类别。
但是,我无法让它在使用 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
视图以指定的任何背景颜色显示。
我尝试在应用蒙版后设置渐变,但没有太大效果。
在代码中
- (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];
}
改变
gradient.frame = self.frame;
到
gradient.frame = self.bounds;
我尝试将辐射应用于自定义形状 UIView
如果在我的代码的其他部分经过良好测试和使用,我使用的渐变类别。
但是,我无法让它在使用 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
视图以指定的任何背景颜色显示。 我尝试在应用蒙版后设置渐变,但没有太大效果。
在代码中
- (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];
}
改变
gradient.frame = self.frame;
到
gradient.frame = self.bounds;