当我点击UIButton saveButton
时,我希望saveButton
将图片从saveIcon
更改为doneIcon
,持续时间为1.2秒。我已经这样实现了:
override func viewDidLoad() {
//create save button
let saveButton = UIButton(frame: CGRectMake(0, preview.frame.maxY + space, buttonSize, buttonSize))
saveButton.setBackgroundImage(UIImage(named: "saveIcon"), forState: .Normal)
saveButton.setBackgroundImage(UIImage(named: "doneIcon"), forState: .Disabled)
saveButton.center.x = returnButton.center.x
saveButton.addTarget(self, action: "save:", forControlEvents: .TouchUpInside)
saveButton.addTarget(self, action: "canSaveAgain:", forControlEvents: .TouchUpInside)
self.view.addSubview(saveButton)
}
func save(sender: UIButton){
UIImageWriteToSavedPhotosAlbum(masterpiece, nil, nil, nil)
sender.enabled = false
self.performSelector("canSaveAgain:", withObject: sender, afterDelay: 1.2)
}
func canSaveAgain(sender: UIButton){
sender.enabled = true
}
虽然正在启用和禁用saveButton
,但即使我在禁用时将其图片设置为saveButton
,doneIcon
也不会更改其图像(即saveButton.setBackgroundImage(UIImage(named: "doneIcon"), forState: .Disabled)
)。
如果有人能告诉我为什么会发生这种情况以及如何暂时改变UIButton
的形象,我将非常感激!
答案 0 :(得分:0)
我发现了我的问题!实际上,当我点击UIButton
时,我有两种方法被调用。也就是说,当我按下按钮时,我正在调用save:
和 canSaveAgain:
。结果,我同时将按钮设置为禁用并立即启用。
我编辑的代码:
override func viewDidLoad() {
super.viewDidLoad()
//create save button
let saveButton = UIButton(frame: CGRectMake(0, preview.frame.maxY + space, buttonSize, buttonSize))
saveButton.setImage(UIImage(named: "saveIcon"), forState: .Normal)
saveButton.setImage(UIImage(named: "doneIcon"), forState: .Disabled)
saveButton.center.x = returnButton.center.x
saveButton.addTarget(self, action: "save:", forControlEvents: .TouchUpInside)
self.view.addSubview(saveButton)
}
func save(sender: UIButton){
UIImageWriteToSavedPhotosAlbum(masterpiece, nil, nil, nil)
sender.enabled = false
self.performSelector("canSaveAgain:", withObject: sender, afterDelay: 1.0)
}
func canSaveAgain(sender: UIButton){
sender.enabled = true
}