扩展类--贝塞尔曲线切割圆角

转载请注明出处!!!  

  如题,在现如今的APP中经常出现切割圆角的图片,但是有时服务器传给我们的图片往往是方图,不含圆角。那就需要我们自己手动切割。切割圆角的方法有好几种。网上这些切割圆角的教程不少,我就不一一细说了。

     在这些方法中,最简单的就是layer属性设置。但是它有个缺点比较影响性能。最好是使用CAShapeLayer和UIBezierPath来实现。

     但是这样一来代码量就多了。为了减少代码和通用性。我就写了个UIView的扩展类。这样只需要一行代码就能切割各种圆角了。

  下面是可以切割角的类型和调用的方法。

typedef NS_ENUM(NSInteger, UILayoutCornerRadiusType) {
    // 全部角都切圆角
    UILayoutCornerRadiusAll = 0,
   
    // 切三个角
    UILayoutCornerRadiusExceptTopLeft = 1,
    UILayoutCornerRadiusExceptTopRight = 2,
    UILayoutCornerRadiusExceptBottomLeft = 3,
    UILayoutCornerRadiusExceptBottomRight = 4,
    
    // 切两个角(同一边)
    UILayoutCornerRadiusTop = 5,
    UILayoutCornerRadiusLeft = 6,
    UILayoutCornerRadiusRight = 7,
    UILayoutCornerRadiusBottom = 8,
    
    // 切一个角
    UILayoutCornerRadiusTopLeft = 9,
    UILayoutCornerRadiusTopRight = 10,
    UILayoutCornerRadiusBottomLeft = 11,
    UILayoutCornerRadiusBottomRight = 12,
    
    // 对角线
    UILayoutCornerRadiusTopLeftToBottomRight = 13,
    UILayoutCornerRadiusTopRightToBottomLeft = 14,
};
- (void)makeCornerWithType:(UILayoutCornerRadiusType) cornerType WithCornerRadius:(CGFloat)cornerRadius;

使用方法很简单:

[self.textLabel makeCornerWithType:UILayoutCornerRadiusTopRightToBottomLeft WithCornerRadius:20.0f];

在末尾我会把附件传上,有什么问题请联系我。

WCYCorner.zip

原文地址:https://www.cnblogs.com/weicyNo-1/p/9066851.html