func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) { if spoken == 0{ spoken += 1 print("speaking finished") self.ImageView.stopAnimating() self.ImageView.image = self.circleImages.last self.ImageView.animationImages = self.circleImages self.ImageView.animationDuration = 1.5 self.ImageView.animationRepeatCount = 0 self.ImageView.startAnimating() do{ try self.recordAndRecognizeSpeech() }catch let error{ print(error) } }else if spoken == 1{ //animation 1 spoken += 1 //animation 1 self.ImageView.image = self.comingOutImages.first self.ImageView.animationImages = self.comingOutImages self.ImageView.animationDuration = 6.0 self.ImageView.animationRepeatCount = 1 self.ImageView.startAnimating() print(String(self.comingOutImages.count) + " #of images") print("should have animated go in" + String(spoken)) }else{ print("done") } }
口述== 0的方法的第一个块,该动画效果很好,但是当到达口述== 1的动画不播放时,它将设置图像,但不播放动画。我一直在环顾四周,我试图在主线程和其他一些东西上运行它。
编辑:
我有这个方法,它是在上面的动画之前的动画,除非将animationRepeatCount设置为0,否则它将不会播放;如果我将此动画设置为除0以外的任何值,则上面的动画会播放,但此动画不会播放。在这种方法中,我将动画重复计数设置为16,然后上面的动画起作用了,但是这个效果没有。我想将此动画设置为repeatCount 0,并在完成语音处理后将上面的动画设置为repeatCount1。我对iOS还是很陌生,有些事情我只是不对。
func handleSend(){ if Thread.isMainThread{ print("send on main thread") }else{ print("send not on main thread") } do{ try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default) try AVAudioSession.sharedInstance().setActive(true) }catch{} //UIView.animate(withDuration: 4.0, animations: { //animation 2 print("second animation") self.imageView.stopAnimating() self.imageView.image = self.talkingImages.last self.imageView.animationImages = self.talkingImages self.imageView.animationDuration = 0.25 self.imageView.animationRepeatCount = 16 self.imageView.startAnimating() print("should have animated") //}) let utterance = AVSpeechUtterance(string: self.finalString) //utterance.voice = AVSpeechSynthesisVoice(language: "en-GB") //utterance.rate = 0.1 self.synthesizer.speak(utterance) }
EDIT2:我发布整个viewController只是为了参考,它不会让我,因为它超出了字符数限制,但是会发布所有动画并说明流程
这是用于触摸图像视图的touchended覆盖方法,也是第一个动画开始的位置。
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { timesRubbed += 1 if timesRubbed == 1{ player?.stop() //Toast(text: keepRubbing).show() self.view.makeToast(keepRubbing, duration: 3.0, position: .top) } if timesRubbed == 2{ player?.stop() //Toast(text: keepAgain).show() self.view.makeToast(keepAgain, duration: 3.0, position: .top) } if timesRubbed == 3{ player?.stop() timesRubbed += 1 playSoundComeOut() CATransaction.begin() CATransaction.setCompletionBlock { print("after animation 1") DispatchQueue.main.async { CATransaction.begin() CATransaction.setCompletionBlock{ print("after circle animation") //UIView.animate(withDuration: 3.0, animations: { //animation 2 print("second animation") //self.imageView.stopAnimating() self.imageView.image = self.talkingImages.last self.imageView.animationImages = self.talkingImages self.imageView.animationDuration = 0.25 self.imageView.animationRepeatCount = 0 self.imageView.startAnimating() print("should have animated") //}) let utterance = AVSpeechUtterance(string: self.greeting) //utterance.voice = AVSpeechSynthesisVoice(language: "en-GB") //utterance.rate = 0.1 self.synthesizer.speak(utterance) //CATransaction.commit() } //UIView.animate(withDuration: 3.0, animations: { //animation 2 print("second animation") self.imageView.stopAnimating() self.imageView.image = self.circleImages.last self.imageView.animationImages = self.circleImages self.imageView.animationDuration = 1.5 self.imageView.animationRepeatCount = 2 self.imageView.startAnimating() print("should have animated") //}) CATransaction.commit() } } //UIView.animate(withDuration: 6.0, animations: { //animation 1 if self.imageView.isFocused{ print("imageview is focused") }else{ print("not focused") } self.imageView.image = self.comingOutImages.last self.imageView.animationImages = self.comingOutImages self.imageView.animationDuration = 6.0 self.imageView.animationRepeatCount = 1 self.imageView.startAnimating() // }) CATransaction.commit() } }
然后在该动画序列的末尾转到SpeechSynthesizer方法,其中
spoken == 0
然后执行上面发布的handleSend()
方法,然后回到上面的口述== 1的SpeechSynthesizer方法
如果您需要更多信息,请告诉我
我对iOS完全陌生,我正在开发具有许多帧动画的应用程序。一切都进行得很好,直到我尝试使用下面发布的方法制作最终动画为止。 ...