如何在视图上添加方形渐变边框-iOS

时间:2018-07-24 05:25:37

标签: ios objective-c gradient calayer cagradientlayer

我想在图像的顶部窗口等视图上应用方形渐变边框: enter image description here

我做了什么:

CAGradientLayer *gradientLayer =  [[CAGradientLayer alloc] init];
gradientLayer.frame = CGRectMake(0, 0, _imgView.bounds.size.width, _imgView.bounds.size.height);
gradientLayer.startPoint = CGPointMake(0.0, 0.5);
gradientLayer.endPoint = CGPointMake(1.0, 0.5);
gradientLayer.colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor];

CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
shapeLayer.lineWidth = 50;
shapeLayer.path = [UIBezierPath bezierPathWithRect:_imgView.bounds].CGPath;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.strokeColor = [UIColor blackColor].CGColor;
gradientLayer.mask = shapeLayer;
[_imgView.layer addSublayer : gradientLayer];

我希望像第一张图片一样具有渐变边框效果。

0 个答案:

没有答案