原文链接:http://www.xuebuyuan.com/1710271.html
如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore框架)。而若要指定某几个角(小于4)为圆角而别的不变时,这种方法就不好用了。
对于这种情况,Stackoverflow上提供了几种解决方案。其中最简单优雅的方案,就是使用UIBezierPath。下面给出一段示例代码。
1 UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)]; 2 view2.backgroundColor = [UIColor redColor]; 3 [self.view addSubview:view2]; 4 5 UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)]; 6 CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; 7 maskLayer.frame = view2.bounds; 8 maskLayer.path = maskPath.CGPath; 9 view2.layer.mask = maskLayer;