iOS: Layer shadow path is not correct

纵饮孤独 提交于 2019-12-11 13:54:57

问题


I have UIView in my design and I need to allow user to change the corner radius. I have designed the testier using @IBDesignable and @IBInspectable.

I am getting the expected shape(cornerRadius curve) upto the 33% of cornerRadius

e.g bounds.size.width = 100, I am getting correct shadow shape if corner radius is upto 33.

If cornerRadius is more than 33 then shadow shape becomes circle irrespective of original shape cornerRadius.

Any suggestion?

Please refer the code and design screenshot below,

Code:

@IBDesignable class DrawingView: UIView {

    @IBInspectable var cornerRadius: CGFloat = 0.0

    @IBInspectable var borderWidth: CGFloat = 0.0
    @IBInspectable var borderColor: UIColor = .clear

    @IBInspectable var shadowRadius: CGFloat = 0.0
    @IBInspectable var shadowOffset: CGSize = .zero
    @IBInspectable var shadowColor: UIColor = .clear

    override func draw(_ rect: CGRect) {
        // Drawing code
        self.layer.cornerRadius = cornerRadius
        self.clipsToBounds = true
        self.layer.borderWidth = borderWidth
        self.layer.borderColor = borderColor.cgColor

        let path = UIBezierPath(roundedRect: self.bounds, cornerRadius: self.cornerRadius)
        self.layer.shadowPath = path.cgPath
        self.layer.shadowRadius = shadowRadius
        self.layer.shadowOffset = shadowOffset
        self.layer.shadowColor = shadowColor.cgColor
        self.layer.masksToBounds = false
        self.layer.shadowOpacity = 1.0
    }
}

Screenshots:

Corner Radius = 20%

Corner Radius >= 33%

来源:https://stackoverflow.com/questions/53832090/ios-layer-shadow-path-is-not-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!