iOS 裁剪View指定的角裁剪

在开发中碰到view的左上角和右上角须要裁剪,具体实现方法以下:spa

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bgView.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(20, 20)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.path = maskPath.CGPath;
self.bgView.layer.mask = maskLayer;
(instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;在这个方法中,第二个参数UIRectCorner是一个枚举类型,即你须要指定裁剪为圆角的view的角
具体枚举值为:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft     = 1 << 0,  //左上角
UIRectCornerTopRight    = 1 << 1, //右上角
UIRectCornerBottomLeft  = 1 << 2, //左下角
UIRectCornerBottomRight = 1 << 3, //右下角
UIRectCornerAllCorners  = ~0UL   //四个角
};

 

若是你想裁剪多个不一样的角,能够用"|"进行组合,传入多个便可。方法中的CAShapeLayer是一个神奇的子类,能够定制不少有趣的UI控件,具体参考code

文章:放肆的使用UIBezierPath和CAShapeLayer画各类图形blog

相关文章
相关标签/搜索