/**
设置任意圆角及边框(实线/虚线)
@param corners 要设置的圆角位置集合
@param radius 圆角半径
@param lineWidth 边框宽度
@param lineColor 边框颜色
@param lineDashPattern 虚线集合
*/
- (void)rounderWithCorners:(UIRectCorner)corners radius:(CGFloat)radius lineWidth:(CGFloat)lineWidth lineColor:(UIColor *_Nullable )lineColor dash:(NSArray<NSNumber *>*_Nullable )lineDashPattern{
///解决masonry布局获取不了正确的frame
[self.superview layoutIfNeeded];
//绘制圆/圆弧---适用于全圆
//UIBezierPath *maskPath = [UIBezierPath bezierPathWithArcCenter:self.bounds radius:iconWidth*0.5 startAngle:0 endAngle:2*M_PI clockwise:YES];
//矩形---圆角
//UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:self.iconImage.width*0.5];
//宽高相等为全圆,不等为椭圆
//UIBezierPath *maskPath = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius,radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
CAShapeLayer *borderLayer = [CAShapeLayer layer];
borderLayer.lineWidth = lineWidth;
borderLayer.strokeColor = lineColor.CGColor;
borderLayer.fillColor = [UIColor clearColor].CGColor;
if (lineDashPattern) {
borderLayer.lineDashPattern = lineDashPattern;
//borderLayer.lineCap = @"butt";
}
borderLayer.path = maskPath.CGPath;
[self.layer insertSublayer:borderLayer atIndex:0];
self.layer.mask = maskLayer;
}
/**
设置任意圆角 --- 性能优化
@param corners 要设置的圆角位置集合
@param radius 圆角半径
*/
- (void)rounderWithCorners:(UIRectCorner)corners radius:(CGFloat)radius{
[self.superview layoutIfNeeded];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius,radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
//maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
/**
设置圆角 --- 性能优化
@param radius 圆角半径
*/
- (void)roundCornerWithRadius:(CGFloat)radius{
[self rounderWithCorners:UIRectCornerAllCorners radius:radius];
}
iOS 高性能圆角及边框(实线/虚线)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...