如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore框架)。而若要指定某几个角(小于4)为圆角而别的不变时,这种方法就不好用了。
一种简单的实现方式 ,用到CAShapeLayer
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(120, 300, 80, 80)];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:redView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
// [UIBezierPath bezierPathWithRoundedRect:<#(CGRect)#> byRoundingCorners:<#(UIRectCorner)#> cornerRadii:<#(CGSize)#>]
// typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
// UIRectCornerTopLeft = 1 << 0,
// UIRectCornerTopRight = 1 << 1,
// UIRectCornerBottomLeft = 1 << 2,
// UIRectCornerBottomRight = 1 << 3,
// UIRectCornerAllCorners = ~0UL
// };可以选择任意几个角
// cornerRadii 圆角的大小
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = redView.bounds;
maskLayer.path = maskPath.CGPath;
redView.layer.mask = maskLayer;