设置UIView圆角
一个UIView圆角的扩展类
- 设置UIView上半部分圆角
- 设置UIView下半部分圆角
- 设置UIView全圆角
代码
UIView+RectCorner.h
@interface UIView (RectCorner)
- (void)setCornerOnTopCornerRaii:(CGFloat)cornerRaii;
- (void)setCorNerOnBottomCornerRaii:(CGFloat)cornerRaii;
- (void)setCornerRadii:(CGFloat)cornerRadii;
@end
UIView+RectCorner.m
@implementation UIView (RectCorner)
- (void)setCornerOnTopCornerRaii:(CGFloat)cornerRaii {
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(cornerRaii, cornerRaii)];
CAShapeLayer *cornerLayer = [[CAShapeLayer alloc] init];
cornerLayer.frame = self.bounds;
cornerLayer.path = maskPath.CGPath;
self.layer.mask = cornerLayer;
}
- (void)setCorNerOnBottomCornerRaii:(CGFloat)cornerRaii {
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(cornerRaii, cornerRaii)];
CAShapeLayer *cornerLayer = [CAShapeLayer layer];
cornerLayer.frame = self.bounds;
cornerLayer.path = maskPath.CGPath;
self.layer.mask = cornerLayer;
}
- (void)setCornerRadii:(CGFloat)cornerRadii {
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadii];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = maskPath.CGPath;
maskLayer.frame = self.bounds;
self.layer.mask = maskLayer;
}
@end