ios - 动画播放与否取决于链接动画的 animationRepeatCount

标签 ios swift animation delegates

我对 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")
        }

    }

口语 == 0 的方法的第一个 block ,该动画工作正常,但是当它到达口语 == 1 的 block 时,动画不播放它设置图像但不播放动画。我一直在环顾四周,我试图在主线程和其他一些东西上运行它。

编辑:

我有这个方法,它是上面动画之前的动画,除非将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仅供引用,它不会让我因为它超过字符限制,但我会发布所有动画并解释流程

这是用于触摸 ImageView 的 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()上面发布的方法,然后返回到上面的 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/

相关文章:

iphone - -[__NSCFArray objectAtIndex :]: index (1) beyond bounds (1)

xcode - Swift 2.0 map "Instance member cannot be used on type"

swift - 与社交媒体分享高分

JavaScript 对绝对定位元素从底部到每个原始位置进行动画处理

javascript - 有人可以帮我制作以下动画吗

ios - Swift:Firebase:如何确保除了我的应用程序之外没有人可以访问我的数据库

ios - 权利不匹配错误 - 从哪里获取详细信息?

ios - Mapkit Center the user Location annotation in another other views 坐标

swift - NSButton 不调用它的 Action

javascript - 使用 SMIL 制作 SVG 动画