我的设计中有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%
角半径> = 33%