通过单击更改 UIButton 的图像 - XCode 6 Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27025759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing image of UIButton via click - XCode 6 Swift
提问by MitchKrendell
Im simply trying to make it so when I click on a UIButton (for which it currently shows the image of a shell), the image changes into something else (in this case, a coin). This is what i tried so far and have not had any success. I cant find anything to do this for Swift.Thanks.
我只是想让当我点击一个 UIButton(它当前显示的是一个外壳的图像)时,图像会变成别的东西(在这种情况下,一个硬币)。这是我迄今为止尝试过的方法,但没有取得任何成功。我找不到任何可以为 Swift 执行此操作的方法。谢谢。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var lblOutput: UILabel!
@IBOutlet weak var lblWin: UILabel!
@IBOutlet weak var lblLost: UILabel!
@IBOutlet weak var lblWinsAmt: UILabel!
@IBOutlet weak var lblLossesAmt: UILabel!
let coin = UIImage(named: "penny_head") as UIImage;
override func viewDidLoad() {
super.viewDidLoad()
//imgShell1.hidden = true //doesnt work
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btnStart(sender: UIButton) {
}
@IBAction func btnShell1(sender: UIButton) {
sender.setImage(coin,forState: UIControlState.Highlighted);
}
回答by A. R. Younce
The way you're setting up the control is incorrect. Assuming you have a button property named btnShell
(and it's the button you want to setup) change your viewDidLoad() method to:
您设置控件的方式不正确。假设您有一个名为的按钮属性btnShell
(并且它是您要设置的按钮),请将您的 viewDidLoad() 方法更改为:
override func viewDidLoad()
{
super.viewDidLoad()
btnShell.setImage(imgShell1, forState:.Normal);
btnShell.setImage(coin, forState:.Highlighted);
}
And then remove the setImage(_:forState:) call from the action method:
然后从 action 方法中删除 setImage(_:forState:) 调用:
@IBAction func btnShell1(sender: UIButton) {
sender.setImage(coin,forState: UIControlState.Highlighted);
}
回答by Antonio
To permanently change the button image on tap, you have to use the .Normal
enum case and not .Highlighted
for the control state:
要在点击时永久更改按钮图像,您必须使用.Normal
枚举大小写而不是.Highlighted
控制状态:
sender.setImage(coin,forState: UIControlState.Normal)
Setting the image for the .Highlighted
state makes the new image appear only when the button is in that state, i.e. when it is tapped.
为.Highlighted
状态设置图像会使新图像仅在按钮处于该状态时出现,即当它被点击时。
回答by Sebastian
If you are looking to change the UIButton
's background image permanently you have to use Antonio's method:
如果您想UIButton
永久更改的背景图像,则必须使用 Antonio 的方法:
sender.setImage(coin,forState: UIControlState.Normal)
This won't change the UIKit
's default highlighting when you tap the button. When you don't want the button to be highlighted when you touch it or have different appearances for different states, then you might be better off using an UIImageView
.
UIKit
当您点击按钮时,这不会更改的默认突出显示。如果您不希望按钮在触摸时突出显示或在不同状态下具有不同外观,那么最好使用UIImageView
.
In viewDidLoad()
:
在viewDidLoad()
:
imgView?.image = imgOne
imgView?.userInteractionEnabled = true
imgView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "changeImage:"))
The function that changes the image:
改变图像的函数:
func changeImage(recognizer: UITapGestureRecognizer){
self.imgView?.image = imgTwo
}