ios - [CAKeyframeAnimation setFromValue :]: unrecognized selector sent to instance

标签 ios swift animation keyframe

我正在尝试使用 keyframeanimate 为按钮设置动画,并使用一个按钮来修改它的tintColor 和按钮宽度约束,我想将其缩放 2 倍,反之亦然,这是我的代码

func InitialAnimationToTutorialButton() {
        UIView.animateKeyframes(withDuration: 1.5, delay: 0, options: [.repeat], animations: {

            UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.1, animations: {
                self.tutorialButton.tintColor = UIColor(rgb: 0xDB4284)
                self.tutorialButtonWidth.constant = 60
                self.view.layoutIfNeeded()
            })

            UIView.addKeyframe(withRelativeStartTime: 0.1, relativeDuration: 0.1, animations: {
                self.tutorialButton.tintColor = UIColor(rgb: 0x554E6E)
                self.tutorialButtonWidth.constant = 30
                self.view.layoutIfNeeded()
            })

            UIView.addKeyframe(withRelativeStartTime: 0.3, relativeDuration: 0.1, animations: {
                self.tutorialButton.tintColor = UIColor(rgb: 0xDB4284)
                self.tutorialButtonWidth.constant = 60
                self.view.layoutIfNeeded()
            })

            UIView.addKeyframe(withRelativeStartTime: 0.4, relativeDuration: 0.1, animations: {
                self.tutorialButton.tintColor = UIColor(rgb: 0x554E6E)
                self.tutorialButtonWidth.constant = 30
                self.view.layoutIfNeeded()
            })

        }) { (finishFlag) in

        }
    }

但是当我运行它时,我遇到了一个异常,如下所示:

由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“-[CAKeyframeAnimation setFromValue:]:无法识别的选择器发送到实例 0x2820e06e0”

最佳答案

我知道如何解决这个错误。 self.view.layoutIfNeeded() 应该超出 block ,所以我这样做并修复了错误,所以这里是正确的代码:

func InitialAnimationToTutorialButton() {
        UIView.animateKeyframes(withDuration: 1.5, delay: 0, options: [.repeat], animations: {

            UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.1, animations: {
                self.tutorialButton.tintColor = UIColor(rgb: 0xDB4284)
                self.tutorialButtonWidth.constant = 60
            })

            UIView.addKeyframe(withRelativeStartTime: 0.1, relativeDuration: 0.1, animations: {
                self.tutorialButton.tintColor = UIColor(rgb: 0x554E6E)
                self.tutorialButtonWidth.constant = 30
            })

            UIView.addKeyframe(withRelativeStartTime: 0.3, relativeDuration: 0.1, animations: {
                self.tutorialButton.tintColor = UIColor(rgb: 0xDB4284)
                self.tutorialButtonWidth.constant = 60
            })

            UIView.addKeyframe(withRelativeStartTime: 0.4, relativeDuration: 0.1, animations: {
                self.tutorialButton.tintColor = UIColor(rgb: 0x554E6E)
                self.tutorialButtonWidth.constant = 30
            })
        self.view.layoutIfNeeded()

        }) { (finishFlag) in

        }
    }

关于ios - [CAKeyframeAnimation setFromValue :]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57542336/

相关文章:

iphone - 使用 CAAnimation 制作图像动画

css - 是否可以使用 mix-blend-mode 在动画 Canvas 后面使用静态背景?

ios - Swift - NSURLSession 在后台下载?

ios - 用于本地化目的的文本连接

ios - 如何以用户可读的形式显示核心数据获取的数据

swift - 后台通知

ios - Swift:如何设置在 CAKeyFrameAnimation 完成时发生的 Action

javascript - 基于多个 ScrollView(s) 为单个 View 设置动画

ios - UITableViewCell 自定义添加到收藏夹按钮问题

iphone - 在设备上测试 iPhone 应用程序的步骤(已创建配置文件/证书)