swift 是否有一种方法可以保存UIButton的“选定”状态?

nhaq1z21  于 2023-08-02  发布在  Swift
关注(0)|答案(3)|浏览(127)

我正在为孩子们创建一个拼图游戏应用程序,在24个不同的场景中有24个不同的小拼图。我把背景音乐放在一个Singleton中,像这样:

class MusicHelper { static let sharedHelper = MusicHelper() var audioPlayerBGM: AVAudioPlayer?
    func playBackgroundMusic() {
        let BGM = URL(fileURLWithPath: Bundle.main.path(forResource: "jigsawBGM", ofType: "wav")!)
        do {
            audioPlayerBGM = try AVAudioPlayer(contentsOf:BGM as URL)
            audioPlayerBGM!.numberOfLoops = -1
            audioPlayerBGM!.prepareToPlay()
            audioPlayerBGM!.play()
            audioPlayerBGM!.volume = 0.1
        } catch {
            print("Cannot play the file")
        }
    }
    
    func pauseBackgroundMusic() {
        let BGM = URL(fileURLWithPath: Bundle.main.path(forResource: "jigsawBGM", ofType: "wav")!)
        do {
            audioPlayerBGM = try AVAudioPlayer(contentsOf:BGM as URL)
            audioPlayerBGM!.pause()
        } catch {
            print("Cannot play the file")
        }
    }
}

字符串
为了让家长们保持理智,我还编写了一段代码来静音BGM。像这样:

@IBOutlet weak var muteMusicButton: UIButton!
@IBAction func muteMusic(_ sender: UIButton) {
    sender.isSelected = !sender.isSelected
 
    if muteMusicButton.isSelected {
        MusicHelper.sharedHelper.pauseBackgroundMusic()
    } else {
        MusicHelper.sharedHelper.playBackgroundMusic()
    }
}


我已经把muteMusicButton放在一个单独的视图控制器场景中(就像应用程序用户的'控件'或'设置'窗口)。这是一个自定义按钮,默认状态(“音乐打开”图像)和选定状态(“音乐关闭”图像)有不同的图像。问题是按钮在其视图控制器被解除时重置为默认状态,因此当您再次访问视图控制器时,音乐仍然暂停,但按钮显示“音乐打开”图像,您必须按两次才能再次开始音乐。
有没有一种方法可以让按钮在您关闭/重新访问其视图控制器时“记住”其选定状态?(也许是UserDefault,我是出了名的无能!)
或者我应该以一种完全不同的方式来处理整个事情?

w46czmvw

w46czmvw1#

与其说“我在UserDefault是垃圾”,不如花点时间去了解它。
不过,非常短暂。。
为您的设置确定一个“键”--让我们使用“isMuted”
当你要开始播放音乐时,从UserDefaults读取该键的值:

let b: Bool = UserDefaults.standard.bool(forKey: "isMuted")
if b {
    // don't play the music
} else {
    // play the music
}

字符串
当你显示你的“设置”视图控制器(例如在viewDidLoad中)时,读取该值并设置按钮的isSelected属性:

let b: Bool = UserDefaults.standard.bool(forKey: "isMuted")
muteMusicButton.isSelected = b


当用户点击按钮时,将新值写入UserDefaults(基于您的代码):

@IBAction func muteMusic(_ sender: UIButton) {
    
    sender.isSelected = !sender.isSelected

    UserDefaults.standard.set(sender.isSelected, forKey: "isMuted")
    
    if muteMusicButton.isSelected {

        MusicHelper.sharedHelper.pauseBackgroundMusic()
    }

    else {

        MusicHelper.sharedHelper.playBackgroundMusic()
    }
}

avwztpqn

avwztpqn2#

听起来最好是读取ViewControllerviewWillAppear处的isPlaying属性,以相应地更新按钮状态。
https://developer.apple.com/documentation/avfaudio/avaudioplayer/1390139-isplaying

override func viewWillAppear(_ animated: Bool) {
   muteMusicButton.selected = !MusicHelper.sharedHelper.audioPlayerBGM.isPlaying
}

字符串

jhdbpxl9

jhdbpxl93#

使用此选项将Bool存储到UserDefaults:

UserDefaults.standard.set(false, forKey: "isMuted")

字符串
用这个来读回你的Bool

let isMuted = UserDefaults.standard.bool(forKey: "isMuted")


在您的示例中:

class MusicHelper {
    static let sharedHelper = MusicHelper()
    var audioPlayerBGM: AVAudioPlayer?

    init() {
        let BGM = URL(fileURLWithPath: Bundle.main.path(forResource: "jigsawBGM", ofType: "wav")!)
        audioPlayerBGM = try? AVAudioPlayer(contentsOf: BGM)
        audioPlayerBGM?.numberOfLoops = -1
        audioPlayerBGM?.volume = 0.1
        audioPlayerBGM?.prepareToPlay()

         let isMuted = UserDefaults.standard.bool(forKey: "isMuted") // here
         if !isMuted {
             audioPlayerBGM?.play()
         }
    }

    func playBackgroundMusic() {
        audioPlayerBGM?.play()
    }

    func pauseBackgroundMusic() {
        audioPlayerBGM?.pause()
    }
}

x

@IBOutlet weak var muteMusicButton: UIButton!

@IBAction func muteMusic(_ sender: UIButton) {

    sender.isSelected = !sender.isSelected
 
    if muteMusicButton.isSelected {
        MusicHelper.sharedHelper.pauseBackgroundMusic()
        UserDefaults.standard.set(false, forKey: "isMuted")  // here
     } else {
        MusicHelper.sharedHelper.playBackgroundMusic()
        UserDefaults.standard.set(true, forKey: "isMuted")  // here
     }
}
func viewWillAppear() {
   muteMusicButton.isSelected = UserDefaults.standard.bool(forKey: "isMuted")
}

的一种或多种

相关问题