Swift - UIView扩展一个圆角设置三个角为圆角
2020-10-16 本文已影响0人
土豆骑士
实现如下UI
三个角圆角
摘自SwifterSwift,设置[.bottomLeft, .topRight]
即可。PS:被设置的View要设置Frame。
public extension UIView {
/// SwifterSwift: Set some or all corners radiuses of view.
///
/// - Parameters:
/// - corners: array of corners to change (example: [.bottomLeft, .topRight]).
/// - radius: radius for selected corners.
func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
let maskPath = UIBezierPath(
roundedRect: bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
let shape = CAShapeLayer()
shape.path = maskPath.cgPath
layer.mask = shape
}
}