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

标签 ios swift uibutton core-graphics uibezierpath

我正在开发我想要制作自定义形状按钮的应用程序,如下所示
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/

相关文章:

ios - 时间戳 413033364.14671 是什么格式?

ios - 尝试在 iOS 中显示表内容时无法从 NSUserDefaults 加载数据

swift - 圆形 UIScrollView – SwiftCarousel : Want to autoscroll

swift - 按下按钮时将 UIViewController .xib 添加到 UIViewController

swift - UITableView CoreData - 排序描​​述符不影响标题

ios - 如何跟踪屏幕渲染器

iphone - 汽车(注释)动画(如 super 应用程序)无法正常工作

iphone - 将 UIButtons 添加到 Cocos2d 项目

ios - 更改 Collection View 中的单个 UIButton 图像会影响多个单元格

ios - 我可以通过标签从 iOS 获取元素吗?