【问题标题】:Redraw CoreGraphics drawing when device orientation changed当设备方向改变时重绘 CoreGraphics 绘图
【发布时间】:2020-11-29 02:06:44
【问题描述】:

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

为此,我使用 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)
    }
    
}

}

现在,当我在应用程序上运行应用程序按钮时,它几乎可以完美运行,就像我想在下面看到的一样

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

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

应用加载时

横向时

当再次纵向

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

【问题讨论】:

    标签: ios swift uibutton core-graphics uibezierpath


    【解决方案1】:

    不要在每次调用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) 
            
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多