Create a rectangle with just two rounded corners in swift?

后端 未结 15 3030
南笙
南笙 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条回答
  •  情书的邮戳
    2020-11-28 04:03

    2017...

    @IBDesignable
    class RoundedEnds: UIView {
        
        override func layoutSubviews() { setup() } // "layoutSubviews" is best
        
        func setup() {
            let r = self.bounds.size.height / 2
            let path = UIBezierPath(roundedRect: self.bounds, cornerRadius:r)
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            self.layer.mask = mask
        }
    }
    

    For only some corners, just change to:

    roundedRect: self.bounds,
      byRoundingCorners: [.topLeft, .topRight],
      cornerRadii: CGSize(width: r, height: r)
    

    Note that as usual there have been many small changes in Swift, eg capitalization of constants, etc.

提交回复
热议问题