在 UIButton 激活后设置延迟

Set a delay after UIButton activation

我正在开发一个骰子游戏,该游戏掷出 4 个骰子,每个骰子都分配有随机数。当我按下滚动按钮时,声音效果按预期播放并且图像被替换,但我正在寻找一种方法来防止用户在声音效果结束(可能 2 秒)之前按下滚动按钮。

这是我更新骰子图像的函数,我一直在通过将 DispatchTime.now() + 2 添加到 if 语句和 arc4random_uniform 之前来测试这个问题,但无济于事:

func updateDiceImages() {
    randomDiceIndex1 = Int(arc4random_uniform(6))
    randomDiceIndex2 = Int(arc4random_uniform(6))
    randomDiceIndex3 = Int(arc4random_uniform(6))
    randomDiceIndex4 = Int(arc4random_uniform(6))
    randomMultiplier = Int(arc4random_uniform(4))

    // determine the operator dice at random
    if randomMultiplier == 0 {
        addDice()
    }
    if randomMultiplier == 1 {
        subDice()
    }
    if randomMultiplier == 2 {
        divDice()
    }
    if randomMultiplier == 3 {
        multDice()
    }

    // image changes to random dice index
    diceImageView1.image = UIImage(named: diceArray[randomDiceIndex1])

    diceImageView2.image = UIImage(named: diceArray[randomDiceIndex2])

    diceImageView3.image = UIImage(named: diceArray[randomDiceIndex3])

    diceImageView4.image = UIImage(named: diceArray[randomDiceIndex4])

    multImageView1.image = UIImage(named: multArray[randomMultiplier])
}     

如有必要,这也是我播放音效的功能,我也尝试实现DispatchTime.now() + 2

func rollSound() {
    // Set the sound file name & extension
    let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "diceRoll", ofType: "mp3")!)

    do {
        // Preparation
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    } catch _ {
    }
    do {
        try AVAudioSession.sharedInstance().setActive(true)
    } catch _ {
    }

    // Play the sound
    do {
        audioPlayer = try AVAudioPlayer(contentsOf: alertSound)
    } catch _{
    }

    audioPlayer.prepareToPlay()
    audioPlayer.play()
}      

这是我认为最接近的实现,但我遇到了很多错误:

    func rollSound() {
    // Set the sound file name & extension
    let when = DispatchTime.now() + 2 // change 2 to desired number of seconds
    DispatchQueue.main.asyncAfter(deadline: when) {
        let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "diceRoll", ofType: "mp3")!)

        do {
            // Preparation
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ {
        }

        // Play the sound
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: alertSound)
        } catch _{
        }

        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }
}

我已经针对您的错误更新了代码。

func rollSound() {
    // Set the sound file name & extension
    let when = DispatchTime.now() + 2 // change 2 to desired number of seconds
    DispatchQueue.main.asyncAfter(deadline: when) {
        let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "diceRoll", ofType: "mp3")!)

        do {
            // Preparation
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
        } catch _ {
        }
        do {
            try AVAudioSession.sharedInstance().setActive(true)
        } catch _ {
        }

        // Play the sound
        do {
            self.audioPlayer = try AVAudioPlayer(contentsOf: alertSound)
        } catch _{
        }

        self.audioPlayer?.prepareToPlay()
        self.audioPlayer?.play()
    }
}

如果要将 class 实例用于 DispatchQueue 更近,则需要将 self 添加到 属性。 updateDiceImages()

也一样