更改设备方向后重绘CoreGraphics绘图

时间:2020-08-09 07:41:45

标签: ios swift uibutton core-graphics uibezierpath

我正在开发想要在下面制作自定义形状按钮的应用程序

enter image description here

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

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

}

现在,当我在应用程序上运行应用程序按钮时,它的运行效果几乎是完美的,如下所示 enter image description here

现在的问题是,当我更改设备的方向时,绘图按钮的形状是相同的,不会刷新。

enter image description here

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

应用加载时

enter image description here

横向时

enter image description here

再次当肖像

enter image description here

请给我解决方案或想法,解决这个麻烦,谢谢。

1 个答案:

答案 0 :(得分:1)

不要在每次调用layerdraw()时都像在调用中那样在setNeedsLayout方法中添加layoutifNeeded ...用于将它们添加到公用init() < / p>

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