简体   繁体   English

UIButton没有在两个图像Swift 4.2之间切换

[英]UIButton not toggling between two images Swift 4.2

I am wanting to add sounds to my app. 我想为我的应用添加声音。 I have added a UIButton with two images, soundON and soundOFF. 我添加了一个带有两个图像的UIButton,soundON和soundOFF。 When I call the sound settings in the app the first time, they toggle fine with each image. 当我第一次在应用程序中调用声音设置时,它们会对每个图像进行精确切换。 However, when I return to the sound settings a second and subsequent time, it is like the soundOff images does not disappear when the soundOn image is displayed. 但是,当我第二次及以后返回声音设置时,就像soundOn图像在显示soundOn图像时不会消失。 Odd as the code is so short and simple. 奇怪的是,代码非常简短。

func soundButton() {
    sounds = UIButton(frame : CGRect(x: 65, y: 70, width: 40, height:40))
    sounds.setImage(UIImage(named : "soundON"), for : .normal)
    sounds.setImage(UIImage(named : "soundOFF"), for : .selected)
    sounds.showsTouchWhenHighlighted = true
    sounds.addTarget(self, action: #selector(soundButtonTapped), for: .touchUpInside)
    self.soundView.addSubview(sounds)
}

@objc func soundButtonTapped(_ sender: Any) {
    sounds.isSelected.toggle()
    isSoundOn.toggle()
}

I have added a video to show the issue as this will save a ton of typing. 我添加了一个视频来显示问题,因为这将节省大量的打字。

http://www.reeflifeapps.com/soundError.mov http://www.reeflifeapps.com/soundError.mov

Any help is greatly appreciated. 任何帮助是极大的赞赏。

update: I had the button on a UIView that was hidden on startup of the puzzle. 更新:我在UIView上有一个按钮,它在拼图启动时被隐藏了。 When the user pressed the "Sounds Settings" icon, the sound setting UIView was unhidden. 当用户按下“声音设置”图标时,声音设置UIView被取消隐藏。 I had the button on this func to unhide the sound settings. 我有这个功能上的按钮取消隐藏声音设置。 I moved it to viewDidLoad() and it fixed it. 我将它移动到viewDidLoad()并修复它。

I suggest you to define a boolean variable that keeps the current state of your button. 我建议你定义一个保持按钮当前状态的布尔变量。 Then, you should set current image of button according to the current state of the variable. 然后,您应该根据变量的当前状态设置按钮的当前图像。

var isSoundOn = false

@objc func soundButtonTapped(_ sender: Any) {
    isSoundOn.toggle()
    if isSoundOn {
        // your logic when sound on (set button selected image, action etc)
    } else {
        // logic when sound off (set button not selected image, action etc)
    }
}

If you still want to use soundButton.isSelected as your boolean variable,do not define images for different states in soundButton.setImage(yourImage, for: .selected) and soundButton.setImage(yourImage, for: .normal) and define them as follows: 如果您仍想使用soundButton.isSelected作为布尔变量,请不要soundButton.setImage(yourImage, for: .selected)soundButton.setImage(yourImage, for: .normal) soundButton.setImage(yourImage, for: .selected)为不同状态定义图像,并将它们定义如下:

soundButton.setImage(soundButton.isSelected ? soundOnImage : soundOffImage, for: .normal)

One of those two approaches above can be used. 可以使用上述两种方法中的一种。

UPDATE: 更新:

As Lloyd Kaijzer stated, isSoundOn = !isSoundOn updated as isSoundOn.toggle() 正如Lloyd Kaijzer所说, isSoundOn = !isSoundOn更新为isSoundOn.toggle()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM