gpt4 book ai didi

swift - 仅在变量输出发生变化时触发事件

转载 作者:行者123 更新时间:2023-11-30 12:15:09 26 4
gpt4 key购买 nike

我创建了以下抛硬币程序,该程序每秒每次抛硬币都会触发一个音频文件。我想知道如何更改此设置,以便仅当结果从正面变为反面时才触发音频,反之亦然。不是像现在这样每一秒。非常感谢任何帮助。

import UIKit
import AVFoundation

class ViewController: UIViewController {

var times: Timer!
var timer: Timer!
var coinFlip : Int = 0
var coinResult : String = ""
var audioPlayer : AVAudioPlayer!
let soundArray = ["note1", "note7"]

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
times = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(fiftyFifty), userInfo: nil, repeats: true)
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(result), userInfo: nil, repeats: true)
result()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func fiftyFifty() -> Int {

coinFlip = Int(arc4random_uniform(2))

return coinFlip
}
func result() -> String {

if coinFlip == 1 {
coinResult = "Heads"
print("Heads")
playSound(soundFileName: soundArray[0])
}
else {
coinResult = "Tails"
print("Tails")
playSound(soundFileName: soundArray[1])
}
}

func playSound(soundFileName : String) {
let soundURL = Bundle.main.url(forResource: soundFileName, withExtension: "wav")
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
}
catch {
print(error)
}
audioPlayer.play()
}
}

我现在对其进行了更改,添加了 result() 的新变量输出 coinResult。不确定这是否有帮助,但希望有帮助。

最佳答案

如果我理解得很好,当硬币翻转时,你想播放一首歌曲,在这种情况下,你可以简单地将 coinFlip 声明更改为:

var coinFlip : Int = 0 {
didSet {
result()
}
}

didSet 是一个属性观察器,用于观察并响应属性值的变化。

didSet is called immediately after the new value is stored.

您可以在Apple's documentation中阅读有关属性观察者的更多信息。 .

<小时/>

编辑:您的代码将如下所示:

import UIKit
import AVFoundation

class ViewController: UIViewController {

var times: Timer!
var timer: Timer!

// Change this
var coinFlip : Int = 0 {
didSet {
result()
}
}

var audioPlayer : AVAudioPlayer!
let soundArray = ["note1", "note7"]

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
times = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(fiftyFifty), userInfo: nil, repeats: true)
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(result), userInfo: nil, repeats: true)
result()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func fiftyFifty() -> Int {

coinFlip = Int(arc4random_uniform(2))

return coinFlip
}
func result() {

if coinFlip == 1 {
print("Heads")
playSound(soundFileName: soundArray[0])
}
else {
print("Tails")
playSound(soundFileName: soundArray[1])
}
}

func playSound(soundFileName : String) {
let soundURL = Bundle.main.url(forResource: soundFileName, withExtension: "wav")
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundURL!)
}
catch {
print(error)
}
audioPlayer.play()
}
}

关于swift - 仅在变量输出发生变化时触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45516373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com