当我单击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
的图像,我将不胜感激! 最佳答案
我发现了我的问题!实际上,单击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
}