Rounded UIView using CALayers - only some corners - How?

后端 未结 14 2232
南方客
南方客 2020-11-22 13:53

In my application - there are four buttons named as follows:

  • Top - left
  • Bottom - left
  • Top - right
  • Bottom - right

Abov

14条回答
  •  不知归路
    2020-11-22 14:19

    I'd suggest defining a layer's mask. The mask itself should be a CAShapeLayer object with a dedicated path. You can use the next UIView extension (Swift 4.2):

    extension UIView {
        func round(corners: UIRectCorner, with radius: CGFloat) {
            let maskLayer = CAShapeLayer()
            maskLayer.frame = bounds
            maskLayer.path = UIBezierPath(
                roundedRect: bounds,
                byRoundingCorners: corners,
                cornerRadii: CGSize(width: radius, height: radius)
            ).cgPath
            layer.mask = maskLayer
       }
    }
    

提交回复
热议问题