Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 动画播放与否取决于链接动画的animationRepeatCount_Ios_Swift_Animation_Delegates - Fatal编程技术网

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

Ios 动画播放与否取决于链接动画的animationRepeatCount,ios,swift,animation,delegates,Ios,Swift,Animation,Delegates,我对iOS完全陌生,我正在开发一个有很多帧动画的应用程序。一切都进行得很顺利,直到我尝试用下面发布的方法制作我的最终动画为止。该方法是指定给内置文本到语音合成器的委托 func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, didFinish utterance: AVSpeechUtterance) { if spoken == 0{ spoken += 1 print

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

    }
方法的第一个块,其中speaked==0,动画工作正常,但当它到达speaked==1的块时,动画不播放,它设置图像,但不播放动画。我一直在四处寻找,我试着在主线程上运行它和其他一些东西

编辑:

我有一种方法,就是在上面的动画之前播放动画,除非将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仅供参考,它不会让我发布,因为它超出了角色限制,但我会发布所有动画并解释流程

这是用于触摸图像视图的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()        }


    }
override func touchesend(touch:Set,带有事件:UIEvent?){
摩擦时间+=1
如果时间摩擦==1{
玩家?.stop()
//Toast(文本:keepRubbing).show()
self.view.makeToast(持续时间:3.0,位置:。顶部)
}
如果时间摩擦==2{
玩家?.stop()
//Toast(文本:keepAgain).show()
self.view.makeToast(keepAgain,持续时间:3.0,位置:。顶部)
}
如果时间摩擦==3{
玩家?.stop()
摩擦时间+=1
PlaySoundComout()
CATransaction.begin()
CATransaction.setCompletionBlock{
打印(“动画1之后”)
DispatchQueue.main.async{
CATransaction.begin()
CATransaction.setCompletionBlock{
打印(“循环动画后”)
//UIView.animate(持续时间:3.0,动画:{
//动画2
打印(“第二个动画”)
//self.imageView.stoppanimating()
self.imageView.image=self.talkingImages.last
self.imageView.animationImages=self.talkingImages
self.imageView.animationDuration=0.25
self.imageView.animationRepeatCount=0
self.imageView.startAnimating()
打印(“应已设置动画”)
//})
let话语=avspeechoutrance(字符串:self.greeting)
//outrance.voice=AVSpeechSynthesisVoice(语言:“en-GB”)
//话语率=0.1
自我。合成器。说话(话语)
//CATransaction.commit()
}
//UIView.animate(持续时间:3.0,动画:{
//动画2
打印(“第二个动画”)
self.imageView.stoppanimating()
self.imageView.image=self.circleImages.last
self.imageView.animationImages=self.circleImages
self.imageView.animationDuration=1.5
self.imageView.animationRepeatCount=2
self.imageView.startAnimating()
打印(“应已设置动画”)
//})
CATransaction.commit()
}
}
//UIView.animate(持续时间:6.0,动画:{
//动画1
如果self.imageView.isFocused{
打印(“图像视图聚焦”)
}否则{
打印(“未聚焦”)
}
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()}
}
然后在动画序列的末尾,它进入SpeechSynthesis方法,其中SpeechSynthesis==0,然后它执行上面发布的
handleSend()
方法,然后返回到SpeechSynthesis方法,其中SpeechSynthesis==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)

同时使用UIView动画块和CATTransaction看起来不太好。请在不使用UIView动画块的情况下重试,并设置CATTransaction的持续时间。不,它仍然不起作用,我取出UIView动画块并放置CATTransaction。在ImageView之前设置AnimationDuration(6.0)不要使用任何CATTransaction内容。您可以使用UIView.animate,它的一个版本有一个完成块。另外,请确保在主线程上运行动画,如以下链接中所示:我尝试在没有CATTransaction的情况下在主线程上运行动画,但它仍然不起作用。当我将动画重复计数更改为0时,它起作用,但它会重复