在開發中碰到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