iOS:图层阴影路径不正确

时间:2018-12-18 11:31:19

标签: ios uiview shadow quartz-core cornerradius

我的设计中有UIView,需要允许用户更改拐角半径。我已经使用@IBDesignable@IBInspectable设计了测试者。

我得到的期望形状(cornerRadius曲线)达到了cornerRadius的33%

e.g bounds.size.width = 100,  如果拐角半径最大为33,我将获得正确的阴影形状。

如果cornerRadius大于33,则阴影形状将变为圆形,而与原始形状cornerRadius无关。

有什么建议吗?

请参考下面的代码和设计屏幕截图,

代码:

@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
    }
}

屏幕截图

角半径= 20%

corenerRadius 20

角半径> = 33%

cornerRadius >= 33

0 个答案:

没有答案
相关问题