日常开发中,可能某些需求需要我们对当前视图进行部分圆角切割,那,我们怎么去处理呢?
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.tipLabel.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.tipLabel.bounds;
maskLayer.path = maskPath.CGPath;
self.tipLabel.layer.mask = maskLayer;
UIRectCorner有五种
UIRectCornerTopLeft // 上左
UIRectCornerTopRight // 上右
UIRectCornerBottomLeft // 下左
UIRectCornerBottomRight // 下右
UIRectCornerAllCorners // 全部
当然我们的项目中相应的封装了相关的方法
/// 设置圆角
/// @param bounds view 的 frame
/// @param cornerRadii 圆角半径 例如:CGSizeMake(15, 15)
/// @param rectCorner 圆角的方向 例如:UIRectCornerTopLeft | UIRectCornerTopRight
- (void)yx_generateCorners:(CGRect)bounds cornerRadii:(CGSize)cornerRadii rectCorner:(UIRectCorner)rectCorner;
- (void)yx_generateCorners:(CGRect)bounds cornerRadii:(CGSize)cornerRadii rectCorner:(UIRectCorner)rectCorner {
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:rectCorner
cornerRadii:cornerRadii];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = bounds;
shapeLayer.path = path.CGPath;
self.layer.mask = shapeLayer;
}
相关使用
[self.shareLabel yx_generateCorners:CGRectMake(0, 0, UI_SCREEN_WIDTH, 50) cornerRadii:CGSizeMake(8, 8) rectCorner:UIRectCornerTopLeft | UIRectCornerTopRight];
注意:使用masonry布局时候,视图的bounds是获取不到的,要标注好相关的CGRectMake
相关学习文章UIBezierPath介绍