我创建的曲线不是我想要的:
这就是我的目标
这是我迄今为止所取得的成就:
class ConerViewBootomRight: UIView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
setup()
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
//This method does the necessary UI adjustments
func setup()
{
if #available(iOS 13.0, *) {
let cornerRadius: CGFloat = 80.0 // Set the corner radius
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: self.bounds.width, y: 0))
path.addLine(to: CGPoint(x: self.bounds.width, y: self.bounds.height - cornerRadius))
path.addQuadCurve(to: CGPoint(x: self.bounds.width - cornerRadius, y: self.bounds.height), controlPoint: CGPoint(x: self.bounds.width, y: self.bounds.height))
path.addLine(to: CGPoint(x: 0, y: self.bounds.height))
path.close()
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
self.layer.mask = maskLayer
}
}
}
请帮助我,我是 swift 的初学者。
您正在初始化程序中调用
setup
。这意味着对 self.bounds.width
和 self.bounds.height
的所有访问都将评估为视图 initial的
bounds
具有的任何维度。如果视图的边界稍后发生变化(例如,通过自动布局约束进行布局),您创建的路径仍将使用旧的宽度和高度。
您应该在
layoutSubviews
中创建路径,以便每次布局视图时都会创建一个新路径(这可能会给它一个不同的 bounds
)。
class ConerViewBootomRight: UIView {
var maskLayer: CAShapeLayer!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
setup()
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
func setup()
{
maskLayer = CAShapeLayer()
self.layer.mask = maskLayer
}
override func layoutSubviews() {
super.layoutSubviews()
let cornerRadius: CGFloat = 80.0
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: self.bounds.width, y: 0))
path.addLine(to: CGPoint(x: self.bounds.width, y: self.bounds.height - cornerRadius))
path.addQuadCurve(to: CGPoint(x: self.bounds.width - cornerRadius, y: self.bounds.height), controlPoint: CGPoint(x: self.bounds.width, y: self.bounds.height))
path.addLine(to: CGPoint(x: 0, y: self.bounds.height))
path.close()
maskLayer.path = path.cgPath
}
}