사용자가 버튼을 누르고 배경색을 변경 (노란색으로)하고 WAV를 재생하고 WAV가 완료되면 버튼이 원래 색상 (빨간색). 따라서 사운드 주변에 완료 핸들러가 있어야합니다. 아래 코드의 다양한 조합을 시도했지만 WAV가 재생되고 단추가 색상을 변경하는 것으로 나타나지 않습니다.스위프트 - 누르기 버튼 (완료 처리기 문제)
잘못된 접근입니까? 아니면 잘못된 것입니까? 완성 된 핸들러를 색상 변경과 관련 지어 놓고 싶지는 않을 것 같습니다. 과장된 생각입니다.
감사합니다.
typealias CompletionHandler = (success:Bool) -> Void
@IBAction func fireButton(sender: AnyObject) {
playLaser({ (success)-> Void in
if success {
self.shots -= 1
self.labelShotsLeft.text = String(self.shots)
} else {
}
})
}
func playLaser(completionHandler: CompletionHandler) {
fireButton.layer.backgroundColor = UIColor.yellowColor().CGColor
let url = NSBundle.mainBundle().URLForResource("laser", withExtension: "wav")!
do {
player = try AVAudioPlayer(contentsOfURL: url)
guard let player = player else { return }
player.prepareToPlay()
player.play()
} catch let error as NSError {
print(error.description)
}
self.fireButton.layer.backgroundColor = UIColor.redColor().CGColor
completionHandler(success: true)
}