Create a rectangle with just two rounded corners in swift?

后端 未结 15 2999
南笙
南笙 2020-11-28 03:14

I need to create a rectangle that have just two rounded corners in swift (Objective C code also ok).

At the moment my code is creating two rectangles with



        
15条回答
  •  Happy的楠姐
    2020-11-28 04:02

    In summary, you can create pretty extension like this:

    extension UIView {
    
        func roundCorners(_ corners: UIRectCorner, radius: Double) {
            let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
            let shape = CAShapeLayer()
            shape.path = maskPath.cgPath
            layer.mask = shape
        }
    
    }
    

    Use it like this:

    view.roundCorners([.topRight, .bottomRight], radius: 10)
    

    Here is all corners values:

    • .topLeft
    • .topRight
    • .bottomLeft
    • .bottomRight

提交回复
热议问题