目标:
给控件添加部分圆角或半边圆角
方案:
用贝塞尔曲线与CAShapeLayer结合使用,将控件的layer层的mask设置为前面操作过的CAShapeLayer
/** 给控件添加部分圆角
* @param rect 控件的rect
* @param corners 需要设置为圆角的角 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerAllCorners
* @param cornerRadii 需要设置的圆角大小 CGSize
* @param roundView 需要设置圆角的控件
**/
- (void)addCornerWithRoundedRect:(CGRect)rect RoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii roundView:(UIView *)roundView{
UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:cornerRadii];
CAShapeLayer* shape = [[CAShapeLayer alloc] init];
[shape setPath:rounded.CGPath];
roundView.layer.mask = shape;
}