我对 iOS 完全陌生,我正在开发一个具有许多帧动画的应用程序。一切都很顺利,直到我尝试用下面发布的方法制作最终动画。该方法是分配给内置文本到语音合成器的委托
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")
}
}
该方法的第一个块,其中spoken == 0,该动画工作正常,但是当它到达spoken == 1的块时,该动画不会播放,它会设置图像,但不会播放动画。我一直在四处寻找,我尝试在主线程和一堆其他东西上运行它。
EDIT:
我有这个方法,它是上面的动画之前的动画,除非将animationRepeatCount设置为0,否则不会播放,如果我将此动画设置为除0之外的任何值,则上面的动画会播放,但此动画不会播放。在这种方法中,我将动画重复计数为 16,然后上面的动画可以工作,但这个不行。我想将此动画设置为repeatCount 0,当语音完成时将上面的动画设置为repeatCount 1。我对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 只是为了参考,它不会让我,因为它超出了字符限制,但我会发布所有动画并解释流程
这是用于触摸图像视图的 touchending 重写方法,它也是第一个动画开始的地方。
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()
上面发布的方法,然后返回到上面的speechSynthesizer方法,其中speech == 1
如果您需要更多信息,请告诉我