在UIButton激活后设置延迟

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在UIButton激活后设置延迟相关的知识,希望对你有一定的参考价值。

我正在制作一个骰子游戏,掷骰子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()
    }
}

如果要将类实例更密切地用于self,则需要将DispatchQueue添加到属性中。和updateDiceImages()一样

以上是关于在UIButton激活后设置延迟的主要内容,如果未能解决你的问题,请参考以下文章

angularJS使用ocLazyLoad实现js延迟加载

片段布局加载延迟

UIbutton不改变IBAction内的背景

Android中切换标签片段之间的延迟

iOS UIButton在将背景图像设置为nil后消失

在 UIScrollView 中以编程方式依次单击 10 个 UIButton