iOS 在reload时候 CAGradientLayer 闪动修改

正常操作:
开始的时候直接在 UIView 上 Add 一个Layer

[self.layer addSublayer:self.vipPriceGradientLayer];
        
- (CAGradientLayer *)vipPriceGradientLayer
{
    if (!_vipPriceGradientLayer) {
        CAGradientLayer *gl = [CAGradientLayer layer];
        gl.startPoint = CGPointMake(0.62, 0);
        gl.endPoint = CGPointMake(0.62, 1);
        gl.colors = @[(__bridge id)[UIColor colorWithHexString:@"#FEE5B6"].CGColor, (__bridge id)[UIColor colorWithHexString:@"#FED28B"].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        gl.cornerRadius = XXBPtScale(5);
        _vipPriceGradientLayer = gl;
    }
    return _vipPriceGradientLayer;
}

解决方法:

+ (Class)layerClass
{
    return [CAGradientLayer class];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        CAGradientLayer *gl = (CAGradientLayer *)self.layer;
        gl.startPoint = CGPointMake(0.62, 0);
        gl.endPoint = CGPointMake(0.62, 1);
        gl.colors = @[(__bridge id)[UIColor colorWithHexString:@"#FEE5B6"].CGColor, (__bridge id)[UIColor colorWithHexString:@"#FED28B"].CGColor];
        gl.locations = @[@(0), @(1.0f)];
        gl.cornerRadius = 5;
        
        self.frame = CGRectMake(0, 0, 0,  20);
        
    }
    return self;
}

转自(感谢大佬):https://duxinfeng.com/post/解决-CAGradientLayer-闪烁问题

你可能感兴趣的:(iOS 在reload时候 CAGradientLayer 闪动修改)