如题,在现如今的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];
在末尾我会把附件传上,有什么问题请联系我。
本文介绍了一种在iOS应用中实现图片圆角效果的方法。通过使用CAShapeLayer和UIBezierPath,可以有效地为图片添加圆角,同时提供了一个UIView的扩展类以简化操作。文章详细列举了多种圆角切割类型,并提供了具体的调用示例。
2681

被折叠的 条评论
为什么被折叠?



