iOS开发 切割指定位置圆角

思路

  • 1、用贝塞尔曲线绘制制定圆角路径
  • 2、创建一个CAShapeLayer,设置shapeLayer.path等于绘制的路径
  • 3、将shapeLayer作为控件的layer.mask

代码

粘贴图片.png

结果

粘贴图片.png
    /**
     参数:
     
     rect        :  被传入View的bounds
     corners     :  圆角的位置(枚举值:左上、左下、右上、右下,可用“|”符号组合使用)
     cornerRadii :  圆角大小(CGSize)
     */
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.cuttingView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
    shapeLayer.frame = self.cuttingView.bounds;
    shapeLayer.path = bezierPath.CGPath;
    self.cuttingView.layer.mask = shapeLayer;
  • 项目中常用一个分类,调用简单
#import "UIView+WJExtend.h"

@implementation UIView(WJBaseExtend)

- (void)wj_roundViewWithCorners:(UIRectCorner)rectCorner
                    cornerRadii:(CGSize)cornerRadii {
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                               byRoundingCorners:rectCorner
                                                     cornerRadii:cornerRadii];
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.frame = self.bounds;
    layer.path = path.CGPath;
    self.layer.mask = layer;
}

@end

你可能感兴趣的:(iOS开发 切割指定位置圆角)