1、最常见的设置圆角的方法无非就是系统的那两句代码
centerView.layer.cornerRadius =5;
centerView.layer.masksToBounds =YES;
2、那么问题来了,如何设置指定圆角位置呢?比如设置右边上下两个圆角?先图再真相封装后的方法如下:
/*
originalView:要修改圆角的view
corners:要修改的圆角取或
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
size:设置的圆角尺寸
return :返回的是蒙板层 mask
*/
- (CAShapeLayer *)getCornerRoundWithSelfView:(UIView *)originalView byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)size{
//绘制左上和左下圆角
UIBezierPath *maskPath = [UIBezierPathbezierPathWithRoundedRect:originalView.boundsbyRoundingCorners:cornerscornerRadii:size];
CAShapeLayer *maskLayer = [[CAShapeLayeralloc]init];
maskLayer.frame = originalView.bounds;
maskLayer.path = maskPath.CGPath;
return maskLayer;
}
至于如何调用,这么来
toolLab.layer.mask = [self getCornerRoundWithSelfView:toolLabbyRoundingCorners:UIRectCornerTopRight |UIRectCornerBottomRightcornerRadii:CGSizeMake(5,5)];
toolLab这个UILabel的其他设置:外甥打灯笼。至于为什么originalView定义的为UIView对象。
凡是能调出来.layer.mask的对象(A): UILabel;UIButton; UIImageView...
A的爸爸是谁?UIView:UIView;
还有不少能调出来.layer.mask的对象,至于能不能设置成功、有兴趣的可以自行尝试。