gpt4 book ai didi

ios - 带有 CAShapeLayer 的 UIView 不显示 CAGradientLayer

转载 作者:可可西里 更新时间:2023-11-01 05:52:57 25 4
gpt4 key购买 nike

我尝试将辐射应用到自定义形状的 UIView

如果在我的代码的其他部分经过良好测试和使用,我会使用渐变类别。但是,我无法让它在使用 CAShapeLayer

创建的自定义形状 View 中正常运行

在我的 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

View 出现时带有分配给它的任何 backgroundColor。我尝试在应用蒙版后设置渐变,但没有太大效果。

最佳答案

在代码中

- (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;

关于ios - 带有 CAShapeLayer 的 UIView 不显示 CAGradientLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27981009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com