UIView with rounded corners and drop shadow?

前端 未结 30 3162
一整个雨季
一整个雨季 2020-11-22 08:00

I’ve been working on an application for a couple of years and received a simple design request: Round the corners on a UIView and add a drop shadow.To do as given below.

30条回答
  •  再見小時候
    2020-11-22 08:14

    Here is my version in Swift 3 for a UIView

    let corners:UIRectCorner = [.bottomLeft, .topRight]
    let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
    let mask = CAShapeLayer()
    
    mask.path = path.cgPath
    mask.fillColor = UIColor.white.cgColor
    
    let shadowLayer = CAShapeLayer()
    shadowLayer.shadowColor = UIColor.black.cgColor
    shadowLayer.shadowOffset = CGSize(width: 0.0, height: 4.0)
    shadowLayer.shadowRadius = 6.0
    shadowLayer.shadowOpacity = 0.25
    shadowLayer.shadowPath = mask.path
    
    self.layer.insertSublayer(shadowLayer, at: 0)
    self.layer.insertSublayer(mask, at: 1)
    

提交回复
热议问题