标签:cornerRadius

共 1 篇相关文章

IT 累计浏览 2,732

为UIView任意角设置圆角

为 UIView 设置圆角在 iOS 开发中非常常见,一句 view.layer.cornerRadius = 10 就能给四个角统一加圆角。但当只需要为任意一个角或某几个角设置圆角时,cornerRadius 就无能为力了。此时可以借助 layer.mask 遮罩来实现:用 UIBezierPath 的 bezierPathWithRoundedRect:byRoundingCorners:cornerRadii: 方法绘制圆角路径,再将其赋给 CAShapeLayer 的 path 作为 mask。

其中 byRoundingCorners 参数对应 UIRectCorner 枚举(NS_OPTIONS),包含 TopLeft、TopRight、BottomLeft、BottomRight 和 AllCorners,支持按位或组合指定需要圆角的角。文章给出的示例在 layoutSubviews 中创建 CAShapeLayer,用 UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft 为三个角设置了 50pt 的圆角,最后把 shapeLayer 赋给 self.layer.mask,即可只对指定角生效。