border渐变色(CAGradientLayer)

2019-12-13  本文已影响0人  huxinwen
- (void)addBorder {
    [self removeBorder];
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bgView.bounds cornerRadius:5*SCALE_2X];
    //遮罩层
    CAShapeLayer *progressLayer = [CAShapeLayer layer];
    progressLayer.frame = self.bgView.bounds;
    progressLayer.fillColor =  [[UIColor clearColor] CGColor];
    progressLayer.strokeColor=[UIColor redColor].CGColor;
    progressLayer.path=path.CGPath;
    //渐变图层
    _borderLayer =  [CAGradientLayer layer];
    _borderLayer.frame = progressLayer.frame;
    [_borderLayer setColors:[NSArray arrayWithObjects:(id)[[UIColor colorWithHexString:@"06fffe"] CGColor],(id)[[UIColor colorWithHexString:@"d3ff4c"] CGColor], nil]];
    [_borderLayer setLocations:@[@0,@1]];
    [_borderLayer setStartPoint:CGPointMake(0, 0)];
    [_borderLayer setEndPoint:CGPointMake(1, 0)];
    [_borderLayer setMask:progressLayer];
    [self.bgView.layer addSublayer:_borderLayer];
}

- (void)removeBorder {
    [_borderLayer removeFromSuperlayer];
    _borderLayer = nil;
}
上一篇下一篇

猜你喜欢

热点阅读