OC
/**
使用CAShapeLayer和UIBezierPath设置圆角
内存的消耗最少,而且渲染快速
*/
- (void)cutCircleLayer:(UIView *)sender {
// 切半圆
UIBezierPath *maskPath =
[UIBezierPath bezierPathWithRoundedRect:sender.bounds cornerRadius:20];
// 切 圆
// UIBezierPath *maskPath =
// [UIBezierPath bezierPathWithRoundedRect:sender.bounds
// byRoundingCorners:UIRectCornerAllCorners
// cornerRadii:sender.bounds.size];
/**
UIRectCornerTopLeft
UIRectCornerTopRight
UIRectCornerBottomLeft
UIRectCornerBottomRight
UIRectCornerAllCorners
*/
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
//设置大小
maskLayer.frame = sender.bounds;
//设置图形样子
maskLayer.path = maskPath.CGPath;
sender.layer.mask = maskLayer;
}
Swift
/**
使用CAShapeLayer和UIBezierPath设置圆角
内存的消耗最少,而且渲染快速
*/
func cutCircleLayer(sender: UIView) {
// 切半圆
let maskPath = UIBezierPath(roundedRect: sender.bounds, cornerRadius: 20);
// 切 圆
//let maskPath = UIBezierPath(roundedRect: sender.bounds, byRoundingCorners: UIRectCorner.AllCorners, cornerRadii: sender.bounds.size);
/**
TopLeft
TopRight
BottomLeft
BottomRight
AllCorners
*/
let maskLayer = CAShapeLayer();
//设置大小
maskLayer.frame = sender.bounds;
//设置图形样子
maskLayer.path = maskPath.CGPath;
sender.layer.mask = maskLayer;
}
学自: FFIB