给控件设置圆角平时大家用的最多应该是layer.cornerRadius这个属性吧 但是这个属性设置成功后是4个角都是圆角类型的;
现在控制器里创建一个imageVIew
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 50)]; imageView.backgroundColor=[UIColor redColor]; [self.view addSubview:imageView];
在Healp.h 中 根据你的控件决定下边的红色字体的类型
/* 适用于label imageView view */ +(void)viewBeizerRect:(CGRect)rect view:(UIView *)view corner:(UIRectCorner)corner cornerRadii:(CGSize)radii; /* 适用于button */ +(void)ControlBeizerRect:(CGRect)rect Control:(UIControl *)Control corner:(UIRectCorner)corner cornerRadii:(CGSize)radii;
在 Healp.m 中
/* 适用于label imageView view */ +(void)viewBeizerRect:(CGRect)rect view:(UIView *)view corner:(UIRectCorner)corner cornerRadii:(CGSize)radii{ UIBezierPath *maskPath= [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radii]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame =view.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer; } /* 适用于button */ +(void)ControlBeizerRect:(CGRect)rect Control:(UIControl *)Control corner:(UIRectCorner)corner cornerRadii:(CGSize)radii{ UIBezierPath *maskPath= [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radii]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame =Control.bounds; maskLayer.path = maskPath.CGPath; Control.layer.mask = maskLayer; }
然后在控制器里调用
[Healp viewBeizerRect:imageView.bounds view:imageView corner:UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)];//设置一个角 [Healp viewBeizerRect:imageVIew.bounds view:imageView corner:UIRectCornerBottomRight|UIRectCornerBottomLeft cornerRadii:CGSizeMake(20, 20)];//设置多个角
就可以了
设置圆角的属性
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0, 左上
UIRectCornerTopRight = 1 << 1,右上
UIRectCornerBottomLeft = 1 << 2,左下
UIRectCornerBottomRight = 1 << 3,右下
UIRectCornerAllCorners = ~0UL 全角
};