作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 iOS 完全陌生,我正在开发一个具有许多帧动画的应用程序。一切都很顺利,直到我尝试用我在下面发布的方法制作我的最终动画。该方法是分配给内置文本到语音合成器的委托(delegate)
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")
}
}
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)
}
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() }
}
spoken == 0
然后它执行
handleSend()
上面发布的方法,然后返回到上面的 SpeechSynthesizer 方法,其中 speak == 1
最佳答案
我最终为最后一个动画做了这个并且它有效
self.imageView.stopAnimating()
self.imageView.image = self.goingInImages.last
self.imageView.layer.removeAllAnimations()
let animation = CAKeyframeAnimation(keyPath: #keyPath(CALayer.contents))
animation.values = self.goingInImages.map {$0.cgImage!}
//animation.keyTimes = [0.0, 0.25, 0.5, 0.75, 1.0]
animation.calculationMode = .discrete
animation.duration = 6.0
animation.repeatCount = 1
self.imageView.layer.add(animation, forKey: nil)
关于ios - 动画播放与否取决于链接动画的 animationRepeatCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60766734/
我是一名优秀的程序员,十分优秀!