效果图
D5A2B2AE-F430-485E-8694-25E72C8B1796.png
调用
CGRect frame = CGRectMake(0, 0, self.frame.size.width, 80);
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor whiteColor];
view.tag = kOffset;
[Utilities setupView:view corners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:10
borderWidth:2 borderColor:[UIColor grayColor]];
[self addSubview:view];
方法详细
/**
指定圆角并带有颜色边框
view:需要添加效果的视图
corner:圆角位置
width:边框宽度
color:边框颜色
*/
+ (void)setupView:(UIView *)view
corners:(UIRectCorner)corner
cornerRadius:(CGFloat)cornerRadius
borderWidth:(CGFloat)width
borderColor:(UIColor *)color {
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
// 边框
CAShapeLayer *borderLayer = [CAShapeLayer layer];
borderLayer.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
borderLayer.lineWidth = width;
borderLayer.strokeColor = color.CGColor;
borderLayer.fillColor = [UIColor clearColor].CGColor;
// 指定圆角
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
byRoundingCorners:corner
cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
maskLayer.path = bezierPath.CGPath;
borderLayer.path = bezierPath.CGPath;
[view.layer insertSublayer:borderLayer atIndex:0];
[view.layer setMask:maskLayer];
}
网友评论