gpt4 book ai didi

ios - 设备方向改变时重绘 CoreGraphics 绘图

转载 作者:行者123 更新时间:2023-12-01 21:27:27 26 4
gpt4 key购买 nike

我正在开发我想要制作自定义形状按钮的应用程序,如下所示
enter image description here
为此,我使用 coregraphics 为按钮绘制该形状,此处为按钮代码
类奖励步骤按钮:UIButton {

@IBInspectable var firstStep: Bool = true{
didSet{
if firstStep{
secondStep = false
thirdStep = false
}
}
}
@IBInspectable var secondStep: Bool = false{
didSet{
if secondStep{
firstStep = false
thirdStep = false
}
}
}
@IBInspectable var thirdStep: Bool = false{
didSet{
if thirdStep{
firstStep = false
secondStep = false
}
}
}
@IBInspectable var buttonColor: UIColor = UIColor.lightGray
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code



let path = UIBezierPath()
let btnLayer = CAShapeLayer()
if firstStep{
path.addArc(withCenter: CGPoint(x: 5, y: 5), radius: 5, startAngle: .pi, endAngle: 3 * .pi / 2, clockwise: true)
path.addLine(to: CGPoint(x: self.bounds.width - 15, y: 0))
path.addLine(to: CGPoint(x: self.bounds.width, y: self.bounds.height / 2))
path.addLine(to: CGPoint(x: self.bounds.width - 15, y: self.bounds.height))
path.addArc(withCenter: CGPoint(x: 5, y: self.bounds.height - 5), radius: 5, startAngle: .pi / 2, endAngle: .pi, clockwise: true)
path.close()
}else if secondStep{
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: self.bounds.width - 15, y: 0))
path.addLine(to: CGPoint(x: self.bounds.width, y: self.bounds.height / 2))
path.addLine(to: CGPoint(x: self.bounds.width - 15, y: self.bounds.height))
path.addLine(to: CGPoint(x: 0, y: self.bounds.height))
path.addLine(to: CGPoint(x: 15, y: self.bounds.height / 2))
path.addLine(to: CGPoint(x: 0, y: 0))
}else{
path.move(to: CGPoint(x: 0, y: 0))
path.addArc(withCenter: CGPoint(x: self.bounds.width - 5, y: 5), radius: 5, startAngle: 3 * .pi / 2, endAngle: 2 * .pi, clockwise: true)
path.addArc(withCenter: CGPoint(x: self.bounds.width - 5, y: self.bounds.height - 5), radius: 5, startAngle: 2 * .pi, endAngle:.pi / 2, clockwise: true)
path.addLine(to: CGPoint(x: 0, y: self.bounds.height))
path.addLine(to: CGPoint(x: 15, y: self.bounds.height / 2))
path.close()
}

btnLayer.path = path.cgPath
btnLayer.fillColor = self.buttonColor.cgColor
self.layer.addSublayer(btnLayer)
self.bringSubviewToFront(self.titleLabel!)
if thirdStep || secondStep{
self.titleEdgeInsets = UIEdgeInsets(top: 0, left: 25, bottom: 0, right: 0)
}else{
self.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
}

}
}
现在,当我在应用程序上运行应用程序按钮时,它几乎可以正常工作,就像我想在下面看到的一样
enter image description here
现在的问题是,当我更改设备的方向时,绘图按钮的形状相同并不会刷新。
enter image description here
所以我用谷歌搜索并尝试找到解决方案,并使用了很多答案
setNeedsDisplay() 方法,所以我也使用该方法,但问题是,它绘制另一个形状或添加另一个图层,而不是删除前一个。看到这个
应用加载时
enter image description here
横向时
enter image description here
当再次肖像
enter image description here
请给我解决方案或想法如何解决这个烂摊子,谢谢。

最佳答案

不要添加 layerdraw()每次调用 setNeedsLayout 时调用的方法或 layoutifNeeded ... 用于共同添加它们 init()

self.layer.addSublayer(btnLayer) 
draw() 中删除此行方法
//MARK:- initializers

required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}

override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}

private func commonInit(){

self.layer.addSublayer(btnLayer)

}

关于ios - 设备方向改变时重绘 CoreGraphics 绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63323764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com