在开发中碰到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;
typedef NS_OPTIONS(NSUInteger, UIRectCorner) { UIRectCornerTopLeft = 1 << 0, //左上角 UIRectCornerTopRight = 1 << 1, //右上角 UIRectCornerBottomLeft = 1 << 2, //左下角 UIRectCornerBottomRight = 1 << 3, //右下角 UIRectCornerAllCorners = ~0UL //四个角 };
若是你想裁剪多个不一样的角,能够用"|"进行组合,传入多个便可。方法中的CAShapeLayer是一个神奇的子类,能够定制不少有趣的UI控件,具体参考code