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

2024-02-11

我对 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

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


我最终在最后一个动画中这样做了并且它有效

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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

动画播放与否取决于链接动画的animationRepeatCount 的相关文章

随机推荐

  • 在内核空间中存储结构体数组,Linux

    我相信我可能有点过度思考这个问题 我的文件系统上有一个文本文件 我在启动时解析该文件并将结果存储到结构数组中 我需要将此数组从用户空间复制到内核空间 copy from user 并且必须让内核随时可以访问此数据 内核空间中的数据需要通过
  • 环境错误:Gmsh 版本必须 >= 2.0

    我是 fipy 的新手 所以如果我问一些应该显而易见的问题 请原谅我的无知 但我无法运行已经存在的 并且在其他机器上工作的 脚本 无法获取EnvironmentError Gmsh version must be gt 2 0 我可能在安装
  • 使用 exec 中的动态 SQL - exec 终止所有用户会话

    环境 SQL Server 2005 2008 我想使用动态 SQL 终止特定数据库上的所有用户连接 我知道其他方法 例如将数据库模式更改为单一 离线等 但希望使用动态 SQL 我试图避免游标 while 循环以及在执行动态 SQL 时使用
  • Julia 中“检测到包的要求无法满足”

    我在向 Julia 添加包时遇到错误 Error 数据框 v1 5 pkg gt add DataFrames Updating registry at C Users julia registries JuliaComputingRegi
  • C99 printf 中的可变前导零

    我正在用 C99 编写一个多精度库 根据编译代码的平台 我选择不同的表示基础 因此 举例来说 假设在平台 X 上系统选择 BASE 100 并且在平台上Y BASE 10000 假设我代表大无符号整数 如下所示 typedef struct
  • 如何测量用于 .NET 远程处理的 IP 端口输入/输出的字节数?

    我正在使用 NET 远程处理将定期状态更新从 Windows 服务检索到 控制器 应用程序中 该应用程序用于显示有关服务正在执行的操作的一些实时统计信息 由此产生的网络流量非常巨大 是更新数据大小的很多倍 所以很明显 我以一种非常低效的方式
  • 无法弄清楚这个“被调用的对象不是函数”C 时间错误

    因此 对于我的学校作业的一部分 我需要找到当前时间 我用它作为参考 http www cplusplus com reference ctime localtime http www cplusplus com reference ctim
  • 在 Less 中连接字符串

    我认为这是不可能的 但我想我问是否有办法 我的想法是 我有一个用于 Web 资源文件夹路径的变量 root img file test css url root file px background image url url 我得到这个结
  • 以编程方式调节 Windows 7 计算机上的音量

    我想知道是否可以使 Tkinter 比例与计算机的音量相对应 换句话说 通过拖动比例尺 电脑的音量会不会变大 这只是我编写的一些随机代码 我正在运行 64 位 Windows 7 操作系统 1 from Tkinter import 2 3
  • constexpr 数组成员是编译时常量吗?

    是代码片段 struct Parameters static constexpr int n 2 static constexpr double v n 4 0 5 0 合法的 C 11 如果是的话 是Parameters v 0 and
  • 使用 ANTLR 解析循环

    我想使用 ANTLR 解析一个简单的类似 matlab 的 for 循环 循环就像 for i 1 8 y i a i i end 我想解析循环并解析8次y i a i i语句 以便对每个语句执行一些操作 我的规则如下 操作在 C 中描述
  • 有没有办法冻结 ES6 Map?

    我正在寻找一种冻结原生 ES6 地图的方法 Object freeze https developer mozilla org en US docs Web JavaScript Reference Global Objects Objec
  • Swift - 交付应用程序上的本地通知图标徽章编号更新在后台

    我试图弄清楚如何在传递本地通知时动态更新图标徽章编号 在安排时注册徽章号码不是一个选项 因为如果我在发送任何通知之前注册两个或多个通知 UIApplication shared applicationIconBadgeNumber this
  • web.config 文件何时“执行”?

    每次加载网站上的页面时 服务器都会执行 读取它吗 创建应用程序时 web config 会加载到内存中 这通常是对应用程序中页面 资源的第一个请求 IIS ASP NET 监视 web config 的更改 如果发生更改 将重新启动您的应用
  • 如何在 ggplot 背景(而不是面板)上添加图像?

    如何使用图像作为 ggplot2 绘图的背景 例如 代码 mtcars gt ggplot aes cyl geom bar theme plot background element rect fill black It results
  • 如何为 rsync SSH 命令自动传递密码?

    我需要去做rsync by ssh并希望自动执行此操作 而无需传递密码ssh手动 使用 sshpass 非交互式 ssh 密码提供程序实用程序 在Ubuntu上 sudo apt get install sshpass rsync 命令 u
  • 开始使用 RxJava 和 sqlite

    我正在学习 RxJava 并且已经阅读了几篇文章和视频 我对 RxJava 所提供的功能感到非常敬畏 所以我认为目前已经了解了响应式编程的全部内容 Dan Lew 的本教程 http blog danlew net 2014 09 15 g
  • mysqldump - 将多个数据库从单独的 mysql 帐户转储到一个文件

    我使用的标准 mysqldump 命令是 mysqldump opt databases dbname host dbhost user dbuser password dbpass gzip gt filename 转储多个数据库 mys
  • 通过performSelectorOnMainThread调用方法与普通方法调用

    谁能告诉我当我使用调用方法时有什么不同performSelectorOnMainThread并在没有执行选择器的情况下调用相同的方法 For Exa void sampleCALL 现在使用这两个场景调用此方法 self performSe
  • 动画播放与否取决于链接动画的animationRepeatCount

    我对 iOS 完全陌生 我正在开发一个具有许多帧动画的应用程序 一切都很顺利 直到我尝试用下面发布的方法制作最终动画 该方法是分配给内置文本到语音合成器的委托 func speechSynthesizer synthesizer AVSpe