设置UIView圆角

来源:互联网 发布:传智软件科技有限公司 编辑:程序博客网 时间:2024/05/16 02:46

设置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
0 0