Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift-按下按钮(完成处理程序问题)_Swift_Completionhandler - Fatal编程技术网

Swift-按下按钮(完成处理程序问题)

Swift-按下按钮(完成处理程序问题),swift,completionhandler,Swift,Completionhandler,我想让用户按下一个按钮,它改变了背景色(黄色),一个WAV播放和完成的WAV按钮恢复到原来的颜色(红色)。因此,在声音周围有一个完成处理程序。我尝试了下面代码的各种组合,但WAV播放,按钮似乎没有改变颜色 这是错误的方法还是我做错了什么?我不想在颜色变化周围放置完成处理程序,因为我认为这是过分的 非常感谢 typealias CompletionHandler = (success:Bool) -> Void @IBAction func fireButton(sender: AnyOb

我想让用户按下一个按钮,它改变了背景色(黄色),一个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)
}

代码不会仅仅因为你说play.play(),就神奇地暂停和等待-那太可怕了!因此,您所谓的完成处理程序根本不是一个完成处理程序。它立即运行-也就是说,只要你开始玩。您的代码对于获取有关音频播放器何时完成播放的信息没有任何作用


为此,您需要配置一个代理,并在音频播放器播放结束时接收它发出的声音。

代码不会仅仅因为您说了play.play(),就神奇地暂停和等待-那太可怕了!因此,您所谓的完成处理程序根本不是一个完成处理程序。它立即运行-也就是说,只要你开始玩。您的代码对于获取有关音频播放器何时完成播放的信息没有任何作用


为此,您需要配置一个代理,并在音频播放器完成播放时接收它发出的声音。

要检测
AVAudioPlayer
完成播放,您需要使用
AVAudioPlayerDelegate

您可能需要这样写:

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.delegate = self //<- Sorry, this was missing in my first post
        player.play()
    } catch let error as NSError {
        print(error.description)
    }
    audioPlayerCompletionHandler = completionHandler
}

var audioPlayerCompletionHandler: CompletionHandler?
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
    self.fireButton.layer.backgroundColor = UIColor.redColor().CGColor
    audioPlayerCompletionHandler?(success: true)
}
func playLaser(completionHandler:completionHandler){
fireButton.layer.backgroundColor=UIColor.yellowColor().CGColor
让url=NSBundle.mainBundle().URLForResource(“laser”,扩展名为:“wav”)!
做{
player=尝试AVAudioPlayer(contentsOfURL:url)
后卫让球员=球员其他{返回}

player.delegate=self/要检测
AVAudioPlayer
完成播放,您需要使用
AVAudioPlayerDelegate

您可能需要这样写:

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.delegate = self //<- Sorry, this was missing in my first post
        player.play()
    } catch let error as NSError {
        print(error.description)
    }
    audioPlayerCompletionHandler = completionHandler
}

var audioPlayerCompletionHandler: CompletionHandler?
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
    self.fireButton.layer.backgroundColor = UIColor.redColor().CGColor
    audioPlayerCompletionHandler?(success: true)
}
func playLaser(completionHandler:completionHandler){
fireButton.layer.backgroundColor=UIColor.yellowColor().CGColor
让url=NSBundle.mainBundle().URLForResource(“laser”,扩展名为:“wav”)!
做{
player=尝试AVAudioPlayer(contentsOfURL:url)
后卫让球员=球员其他{返回}

player.delegate=self/这是一个比看上去更微妙的问题。我尝试在每个任务周围放置三个完成处理程序:将颜色更改为黄色,播放声音,将颜色更改为红色。代码在我记录时按正确的顺序执行,但由于scre,按钮从未更改颜色更新控件。下面是我希望其他读者会发现有用的代码:

Swift 2.0

@IBAction func fireButton(sender: AnyObject) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        dispatch_sync(dispatch_get_main_queue()) {
            self.fireButton.layer.backgroundColor = UIColor.yellowColor().CGColor
        }
        self.playLaser( { (success)-> Void in
            if success {
                self.shots -= 1
            } else {
            }
        })
        dispatch_sync(dispatch_get_main_queue()) {
            self.labelShotsLeft.text = String(self.shots)
            self.fireButton.layer.backgroundColor = UIColor.redColor().CGColor
        }
    }
}

func playLaser(completion: (success: Bool) -> ()) {
    let url = NSBundle.mainBundle().URLForResource("laser", withExtension: "wav")!
    do {
        player = try AVAudioPlayer(contentsOfURL: url)
        guard let player = player else { return }
        player.play()
        completion(success: true)
        } catch let error as NSError {
        completion(success: false)
    }
}
@IBAction func fireButton(_ sender: AnyObject) {
    let fireQueue = DispatchQueue(label: "queueFirebutton")
    fireQueue.async {
        DispatchQueue.main.sync {
            self.fireButtonDisabled()
        }
        DispatchQueue.main.sync {
            self.playLaser()
            self.shots -= 1
            if self.shots <= 0 {
                self.shots = 0
            }
        }
        DispatchQueue.main.sync {
            if self.shots < 0 { self.shots = 0}
            self.labelShotsLeft.text = String(self.shots)
            sleep(1)
            self.fireButtonEnabled()
        }
    }
}

func playLaser() {
    let url = Bundle.main.url(forResource: "laser", withExtension: "wav")!
    do {
        player = try AVAudioPlayer(contentsOf: url)
        guard let player = player else { return }
        player.play()
    } catch {
    }
}
Swift 3.0

@IBAction func fireButton(sender: AnyObject) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        dispatch_sync(dispatch_get_main_queue()) {
            self.fireButton.layer.backgroundColor = UIColor.yellowColor().CGColor
        }
        self.playLaser( { (success)-> Void in
            if success {
                self.shots -= 1
            } else {
            }
        })
        dispatch_sync(dispatch_get_main_queue()) {
            self.labelShotsLeft.text = String(self.shots)
            self.fireButton.layer.backgroundColor = UIColor.redColor().CGColor
        }
    }
}

func playLaser(completion: (success: Bool) -> ()) {
    let url = NSBundle.mainBundle().URLForResource("laser", withExtension: "wav")!
    do {
        player = try AVAudioPlayer(contentsOfURL: url)
        guard let player = player else { return }
        player.play()
        completion(success: true)
        } catch let error as NSError {
        completion(success: false)
    }
}
@IBAction func fireButton(_ sender: AnyObject) {
    let fireQueue = DispatchQueue(label: "queueFirebutton")
    fireQueue.async {
        DispatchQueue.main.sync {
            self.fireButtonDisabled()
        }
        DispatchQueue.main.sync {
            self.playLaser()
            self.shots -= 1
            if self.shots <= 0 {
                self.shots = 0
            }
        }
        DispatchQueue.main.sync {
            if self.shots < 0 { self.shots = 0}
            self.labelShotsLeft.text = String(self.shots)
            sleep(1)
            self.fireButtonEnabled()
        }
    }
}

func playLaser() {
    let url = Bundle.main.url(forResource: "laser", withExtension: "wav")!
    do {
        player = try AVAudioPlayer(contentsOf: url)
        guard let player = player else { return }
        player.play()
    } catch {
    }
}
@iAction func fireButton(\uSender:AnyObject){
让fireQueue=DispatchQueue(标签:“queueFirebutton”)
fireQueue.async{
DispatchQueue.main.sync{
self.fireButtonDisabled()
}
DispatchQueue.main.sync{
self.playLaser()
self.shots-=1

如果self.shots这是一个比看上去更微妙的问题。我尝试在每个任务周围放置三个完成处理程序:将颜色更改为黄色,播放声音,将颜色更改为红色。代码在我记录时按正确的顺序执行,但按钮从未因屏幕更新而更改颜色控件。以下是我希望其他读者会发现有用的代码:

Swift 2.0

@IBAction func fireButton(sender: AnyObject) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        dispatch_sync(dispatch_get_main_queue()) {
            self.fireButton.layer.backgroundColor = UIColor.yellowColor().CGColor
        }
        self.playLaser( { (success)-> Void in
            if success {
                self.shots -= 1
            } else {
            }
        })
        dispatch_sync(dispatch_get_main_queue()) {
            self.labelShotsLeft.text = String(self.shots)
            self.fireButton.layer.backgroundColor = UIColor.redColor().CGColor
        }
    }
}

func playLaser(completion: (success: Bool) -> ()) {
    let url = NSBundle.mainBundle().URLForResource("laser", withExtension: "wav")!
    do {
        player = try AVAudioPlayer(contentsOfURL: url)
        guard let player = player else { return }
        player.play()
        completion(success: true)
        } catch let error as NSError {
        completion(success: false)
    }
}
@IBAction func fireButton(_ sender: AnyObject) {
    let fireQueue = DispatchQueue(label: "queueFirebutton")
    fireQueue.async {
        DispatchQueue.main.sync {
            self.fireButtonDisabled()
        }
        DispatchQueue.main.sync {
            self.playLaser()
            self.shots -= 1
            if self.shots <= 0 {
                self.shots = 0
            }
        }
        DispatchQueue.main.sync {
            if self.shots < 0 { self.shots = 0}
            self.labelShotsLeft.text = String(self.shots)
            sleep(1)
            self.fireButtonEnabled()
        }
    }
}

func playLaser() {
    let url = Bundle.main.url(forResource: "laser", withExtension: "wav")!
    do {
        player = try AVAudioPlayer(contentsOf: url)
        guard let player = player else { return }
        player.play()
    } catch {
    }
}
Swift 3.0

@IBAction func fireButton(sender: AnyObject) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
        dispatch_sync(dispatch_get_main_queue()) {
            self.fireButton.layer.backgroundColor = UIColor.yellowColor().CGColor
        }
        self.playLaser( { (success)-> Void in
            if success {
                self.shots -= 1
            } else {
            }
        })
        dispatch_sync(dispatch_get_main_queue()) {
            self.labelShotsLeft.text = String(self.shots)
            self.fireButton.layer.backgroundColor = UIColor.redColor().CGColor
        }
    }
}

func playLaser(completion: (success: Bool) -> ()) {
    let url = NSBundle.mainBundle().URLForResource("laser", withExtension: "wav")!
    do {
        player = try AVAudioPlayer(contentsOfURL: url)
        guard let player = player else { return }
        player.play()
        completion(success: true)
        } catch let error as NSError {
        completion(success: false)
    }
}
@IBAction func fireButton(_ sender: AnyObject) {
    let fireQueue = DispatchQueue(label: "queueFirebutton")
    fireQueue.async {
        DispatchQueue.main.sync {
            self.fireButtonDisabled()
        }
        DispatchQueue.main.sync {
            self.playLaser()
            self.shots -= 1
            if self.shots <= 0 {
                self.shots = 0
            }
        }
        DispatchQueue.main.sync {
            if self.shots < 0 { self.shots = 0}
            self.labelShotsLeft.text = String(self.shots)
            sleep(1)
            self.fireButtonEnabled()
        }
    }
}

func playLaser() {
    let url = Bundle.main.url(forResource: "laser", withExtension: "wav")!
    do {
        player = try AVAudioPlayer(contentsOf: url)
        guard let player = player else { return }
        player.play()
    } catch {
    }
}
@iAction func fireButton(\uSender:AnyObject){
让fireQueue=DispatchQueue(标签:“queueFirebutton”)
fireQueue.async{
DispatchQueue.main.sync{
self.fireButtonDisabled()
}
DispatchQueue.main.sync{
self.playLaser()
self.shots-=1
如果是自拍