方法一:maskedCorners
self.layerView.layer.cornerRadius=16
self.layerView.layer.maskedCorners=[.layerMinXMaxYCorner,.layerMaxXMaxYCorner]
CACornerMask 可以分别指定四个角:
public struct CACornerMask : OptionSet {
public init(rawValue: UInt)
public static var layerMinXMinYCorner: CACornerMask { get }
public static var layerMaxXMinYCorner: CACornerMask { get }
public static var layerMinXMaxYCorner: CACornerMask { get }
public static var layerMaxXMaxYCorner: CACornerMask { get }
}
方法二:UIBezierPath
let path = UIBezierPath(roundedRect: self.layerView.bounds, byRoundingCorners: [UIRectCorner.topLeft, UIRectCorner.topRight], cornerRadii: CGSize(width: 16, height: 16))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.layerView.bounds
maskLayer.path = path.cgPath
self.layerView.layer.mask = maskLayer
UIRectCorner 也可以分别指定四个角
public struct UIRectCorner : OptionSet {
public init(rawValue: UInt)
public static var topLeft: UIRectCorner { get }
public static var topRight: UIRectCorner { get }
public static var bottomLeft: UIRectCorner { get }
public static var bottomRight: UIRectCorner { get }
public static var allCorners: UIRectCorner { get }
}