是否播放动画取决于链式动画的animationRepeatCount [英] Animation playing or not depending on animationRepeatCount for chained animations

查看:148
本文介绍了是否播放动画取决于链式动画的animationRepeatCount的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对iOS完全陌生,我正在开发具有许多帧动画的应用程序。一切都进行得很好,直到我尝试使用下面发布的方法制作最终动画为止。该方法是分配给内置文本到语音合成器的委托

I am totally new to iOS and I am working on an application that has many frame animations. Everything is going well until I try to do my final animation in a method that I am posting below. The method is a delegate assigned to the Built in Text to Speech synthesizer

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,则动画无法播放设置图像,但不播放动画。我一直在四处寻找,我试图在主线程和其他东西上运行它。

The firs block of the method where spoken == 0, that animation works fine, but when it gets to the block where spoken == 1 that animation does not play it sets the image but it does not play the animation. Ive been looking around forever, I tried to run it on the main thread and a bunch of other things.

编辑:

我有这个方法,它是在上面的动画之前的动画,除非将animationRepeatCount设置为0,否则它将不播放;如果我将此动画设置为除0之外的任何值,则上面的动画将播放但那么该动画就不会播放。在这种方法中,我将动画重复计数设置为16,然后上述动画有效,但该动画无效。我想将此动画设置为repeatCount 0,并在完成语音处理后将上面的动画设置为repeatCount1。我对iOS非常陌生,有些事情我只是不对。

I have this method which is the animation right before the animation above that will not play unless set animationRepeatCount to 0, and if I set this animation to anything but 0 then the animation above plays but then this animation doesn't play. In this method I have animation repeat count to 16 and then the above animation works but this one doesn't. I would like to have this animation set to repeatCount 0 and when speech is done to fire the one above to repeatCount 1. Im very new to iOS and there is something I am just not getting right.

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覆盖方法,也是第一个动画开始的地方。

this is the touchended override method for touching the image view, it is also where the first animation starts.

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

then at the end of that animation sequence it goes to the speechSynthesizer method where spoken == 0 then it does the handleSend() method posted above and then it goes back to the speechSynthesizer method above where spoken == 1

如果您需要更多信息,请告诉我

If you need any more information please let me know

推荐答案

我结束了修改了最后一个动画的效果,并且效果很好

I ended up doing this for the last animation and it worked

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)

这篇关于是否播放动画取决于链式动画的animationRepeatCount的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆