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

Redraw CoreGraphics drawing when device orientation changed

我正在开发应用程序,我想制作如下所示的自定义形状按钮

为此,我正在使用 coregraphics 绘制按钮的形状和此处的按钮代码

class RewardStepsButton: 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)
    }
    
}

}

现在,当我 运行 应用程序按钮按照我想要的方式在应用程序上绘制时,它几乎可以完美运行,如下所示

现在的问题是,当我改变设备的方向时,绘图按钮的形状是一样的,但不会刷新。

所以我用谷歌搜索并尝试找到解决方案,并且使用了很多答案 setNeedsDisplay() 方法,所以我也使用该方法,但问题是,它绘制另一个形状或添加另一个图层而不是擦除前一个。看到这个

应用加载时

横向时

何时再来人像

请给我解决方案或想法如何解决这个烂摊子,谢谢。

不要在每次调用 setNeedsLayoutlayoutifNeeded 时在 draw() 方法中添加 layer ...用于将它们共同添加 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) 
        
    }