关键代码如下:
self.exitBtn.layer.masksToBounds = YES;
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.exitBtn.bounds byRoundingCorners: UIRectCornerTopRight cornerRadii: (CGSize){8.0f, 8.0f}].CGPath;
self.exitBtn.layer.mask = maskLayer;
其中UIRectCornerTopRight即为设置圆角的位置:上右,同时我们可以看到苹果API中的位移枚举如下:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
故我们可以随心所欲地设置任意控件任意位置的圆角。ps:8.0f为设置圆角大小。