我正在尝试更改按钮单击时的图像,我正在使用 didset 来执行此操作,当变量发生更改时,图像也会更改,即使变量正在更改,它也无法正常工作,这是我的代码:
变量也发生了变化:
var page:Int=1 {
didSet {
DispatchQueue.main.async { [self] in
let newW = page == 1 ? 0.04*Double(Float(bounds.size.width)) : 0.12*Double(Float(bounds.size.width))
let newX=CGFloat(Float(Int(Float(Double(bounds.size.width)*0.5-newW*0.5))))
currOption = UIImageView(image:UIImage(named: options[page-1].imagePath))
currOption.frame = CGRect(x: newX, y: CGFloat(Int(Float(bounds.size.height/22))), width: CGFloat(newW), height: CGFloat(globW))
}
print("2nd Triggered", currOption)
}
}
受影响的变量:
var currOption:UIImageView! = UIImageView(image:UIImage(named: "art.scnassets/A.png"))
其中资产字符串 i:
var options:[AButtons]=[
AButtons(imagePath: "art.scnassets/A.png", name: "A"),
AButtons(imagePath: "art.scnassets/B.png", name: "B"),
AButtons(imagePath: "art.scnassets/C.png", name: "C"),
AButtons(imagePath: "art.scnassets/D.png", name: "D"),
AButtons(imagePath: "art.scnassets/E.png", name: "E"),
]
触发页面变化的函数:
@objc func optionClicked(sender: UIButton){
page = sender.tag + 1
print("1st Trigger",page, sender.tag)
}
所有选项对象都有一个标签感谢大家的时间