[英]Change the UIButton Image when tapped multiple times
My requirement is to change the UIButton image for three different states. 我的要求是将UIButton图像更改为三种不同的状态。 As of now when I tap the button for the first two times (first two states), it works. 到目前为止,当我点击按钮的前两次(前两个状态)时,它可以工作。 But when I tap for the third time or when it triggers my third state, the second image still seems to appear. 但是当我第三次点击或触发我的第三状态时,第二张图像似乎仍然出现。 How can and solve this issue? 如何解决这个问题? My code is below. 我的代码如下。
@IBAction func repeateButtonPressed(_ sender: Any) {
let image = UIImage(named : "repeat_one")
let image2 = UIImage(named : "repeat")
if(playerRepeatMode == RepeatModes.NO_REPEAT){
playerRepeatMode = RepeatModes.REPEAT_LIST
self.repeatButton.setBackgroundImage(image2, for: .normal)
self.repeatButton.backgroundColor = buttonToggleColor
}
else if(playerRepeatMode == RepeatModes.REPEAT_LIST){
playerRepeatMode = RepeatModes.REPEAT
self.repeatButton.setBackgroundImage(image, for: .normal)
// self.repeatButton.setBackgroundImage(#imageLiteral(resourceName: "repeat_one"), for: UIControlState.normal)
self.repeatButton.backgroundColor = buttonToggleColor
}else{
playerRepeatMode = RepeatModes.NO_REPEAT
self.repeatButton.setBackgroundImage(image2, for: .normal)
self.repeatButton.backgroundColor = UIColor.clear
}
}
Answer after question edit 问题后回答编辑
A couple tips to make it easier to debug... 一些提示,使调试变得更容易...
Add comments 添加评论
Use variable names that make sense - eg imgRepeat
instead of image2
for an image named "repeat" 使用有意义的变量名-例如,对于名为“ repeat”的图像,使用imgRepeat
而不是image2
The following code works fine for me - assuming the images have transparent areas to show the background color. 以下代码对我来说效果很好-假设图像具有透明区域以显示背景色。
@IBAction func repeateButtonPressed(_ sender: Any) {
let imgRepeatOne = UIImage(named : "repeat_one")
let imgRepeat = UIImage(named : "repeat")
if (playerRepeatMode == RepeatModes.NO_REPEAT) {
// currently NO_REPEAT, changing to REPEAT_LIST
playerRepeatMode = RepeatModes.REPEAT_LIST
// set BackroundImage to "repeat"
self.repeatButton.setBackgroundImage(imgRepeat, for: .normal)
// set BackgroundColor to buttonToggleColor
self.repeatButton.backgroundColor = buttonToggleColor
} else if (playerRepeatMode == RepeatModes.REPEAT_LIST) {
// currently REPEAT_LIST, changing to REPEAT
playerRepeatMode = RepeatModes.REPEAT
// set BackroundImage to "repeat_one"
self.repeatButton.setBackgroundImage(imgRepeatOne, for: .normal)
// set BackgroundColor to buttonToggleColor
self.repeatButton.backgroundColor = buttonToggleColor
} else {
// currently REPEAT, changing to NO_REPEAT
playerRepeatMode = RepeatModes.NO_REPEAT
// set BackroundImage to "repeat"
self.repeatButton.setBackgroundImage(imgRepeat, for: .normal)
// set BackgroundColor to clear
self.repeatButton.backgroundColor = UIColor.clear
}
}
Original Answer 原始答案
I think the issue is that you are using .setImage()
in one place, but .setBackgroundImage()
in the other two places. 我认为这个问题是您正在使用.setImage()
在同一个地方,但.setBackgroundImage()
中的其他两个地方。
That would be fine, assuming you want image2
to be used as background and image
to be used as the button image, but you need to clear the other property. 假设您希望将 image2
用作背景,并将image
用作按钮图像,那很好,但是您需要清除另一个属性。
So, where you use the background image: 因此,在使用背景图片的地方:
self.repeatButton.setBackgroundImage(image2, for: .normal)
// also *clear* the button image
self.repeatButton.setImage(nil, for: .normal)
and, where you use the button image: 并且,在使用按钮图像的位置:
self.repeatButton.setImage(image, for: .normal)
// also *clear* the background image
self.repeatButton.setBackgroundImage(nil, for: .normal)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.