각자에게 할당 된 임의의 숫자로 4 개의 주사위를 굴리는 주사위 게임을하고 있습니다. 롤 버튼을 누르면 음향 효과가 의도 한대로 재생되고 이미지가 대체되지만 음향 효과가 끝날 때까지 (아마도 2 초) 사용자가 롤을 누르지 못하도록하는 방법을 찾고 있습니다. UIButton 활성화 후 지연 설정
이
내가 if 문 및arc4random_uniform
이전에
DispatchTime.now() + 2
를 추가하여이 문제를 테스트 한 경우, 주사위 이미지를 업데이트 내 기능이지만, 아무 소용이 :
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()
}
}
확인하셨습니까? https://stackoverflow.com/questions/38031137/how-to-program-a-delay-in-swift-3? –
예, 실제로 DispatchTime.now() + 2를 사용하는 조언이있는 곳입니다. OP – jacobbullon
사용 방법을 보여줄 수 있습니까? –