iOS

iOS - Partial Corner

2021-06-27  本文已影响0人  ienos

裁剪 CALayer 或 UIView 的部分圆角

image.png
/*
  @RoundingCorners: 
      - .topLeft: 左上
      - .topRight: 右上
      - .bottomLeft: 左下
      - .bottomRight: 右下

  @radii: 圆角大小
*/
imageView.corner(byRoundingCorners: [.topLeft, .topRight], radii: 10)

扩展实现

// CALayer
extension CALayer {
    func corner(byRoundingCorners corners: UIRectCorner, radii: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.mask = maskLayer
    }
}

// UIView
extension UIView {
    func corner(byRoundingCorners corners: UIRectCorner, radii: CGFloat) {
        self.layer.corner(byRoundingCorners: corners, radii: radii)
    }
}
上一篇下一篇

猜你喜欢

热点阅读