渐变色CAGradientLayer 无效

本来几个渐变色我都是直接复制粘贴

如下
// An highlighted block
let gradientLayer = CAGradientLayer()
            labelview.layoutIfNeeded() // 重点
        gradientLayer.colors = [ UIColor.init(hexString: "#FF941A").cgColor,UIColor.init(hexString: "#FF7037").cgColor]
        gradientLayer.locations = [0.0, 1.0]
        gradientLayer.startPoint = CGPoint.init(x: 0, y: 0.5)
        gradientLayer.endPoint = CGPoint.init(x: 1, y: 0.5)
        gradientLayer.frame = self.labelview.bounds
        gradientLayer.masksToBounds = true
        self.labelview.layer.addSublayer(gradientLayer)

后来发现ui画的全是渐变色btn 干脆封装了一个方法

原谅我swift的分类不会写。
渐变色CAGradientLayer  无效

如下    放到UIView的category就行
//  写法
你的view.setViewBackColors(RGBColor(color: "ED7F67"), end: RGBColor(color: "EA6458"), andLocations: [0,1])

// 
-(void)setViewBackColors:(UIColor *)startColor endColor:(UIColor *)endColor andLocations:(NSArray *)locations{
//渐变颜色
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[(__bridge id)startColor.CGColor, (__bridge id)endColor.CGColor];
    gradientLayer.locations = locations;
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 1.0);
    gradientLayer.frame = self.bounds;
    gradientLayer.cornerRadius = self.layer.cornerRadius;
    [self.layer insertSublayer:gradientLayer atIndex:0];

}
发现没鸟用,明明之前直接粘贴就可以的

渐变色CAGradientLayer  无效

经群里大佬指点  draw重绘一下

渐变色CAGradientLayer  无效

OK了    

渐变色CAGradientLayer  无效

上一篇:iOS 15适配总结


下一篇:2021-05-29