iOS开发之UIView的指定角为圆角
在开发中,当一个UIView需要4个角都是圆角的时候我们可以通过CornerRadius来设置圆角.
当我们有时候只是需要某些角为圆角的时候,可以通过下面的方法轻松添加圆角遮罩
UIView * View = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
View.center = self.view.center;
View.backgroundColor = [UIColor redColor];
[self.view addSubview:View];
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:View.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(50,10)];
CAShapeLayer *markLayer = [[CAShapeLayer alloc]init];
markLayer.frame = View.bounds;
markLayer.path = path.CGPath;
View.layer.mask = markLayer;
UIRectCorner
指定需要成为圆角的角,从名字很容易就可以看出来.想要多个角是圆角的话,可以使用 " | "组合起来就可以了.
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};