iOS 设置渐变色圆角边框
*如下需求图,使用背景图片很难达到很好的效果
WX20220505-101437.png
*就需要使用代码来绘制渐变色圆角边框
/**
* 给view设置渐变色圆角边框
*
* @param view : 要添加边框的view
* @param cornerRadius : 圆角大小
* @param lineWidth : 线宽
* @param colors : 渐变颜色数组
* colors : @[(__bridge id)RGB_COLOR(117, 48,227, 1).CGColor,(__bridge id)RGB_COLOR(225, 175, 204, 1).CGColor]
*/
- (void)addGradientLayerWithCorner:(UIView *)view withCornerRadius:(float)cornerRadius withLineWidth:(float)lineWidth withColors:(NSArray *)colors{
CGRect mapRect = CGRectMake(lineWidth/2, lineWidth/2, view.frame.size.width-lineWidth, view.frame.size.height-lineWidth);
CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
gradientLayer.colors = colors;
gradientLayer.startPoint = CGPointMake(0, 0.5);
gradientLayer.endPoint = CGPointMake(1, 0.5);
gradientLayer.cornerRadius = mapRect.size.height/2;
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.lineWidth = lineWidth;
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:mapRect cornerRadius:mapRect.size.height/2];
maskLayer.path = path.CGPath;
maskLayer.fillColor = [UIColor clearColor].CGColor;
maskLayer.strokeColor = [UIColor blueColor].CGColor;
gradientLayer.mask = maskLayer;
[view.layer addSublayer:gradientLayer];
}
网友评论