[CAKeyframeAnimation setFromValue:]: 无法识别的选择器发送到实例

[CAKeyframeAnimation setFromValue:]: unrecognized selector sent to instance

我正在尝试使用 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

        }
    }

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

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CAKeyframeAnimation setFromValue:]: unrecognized selector sent to instance 0x2820e06e0'

我明白了如何解决这个错误。 self.view.layoutIfNeeded() 应该没有块,所以我这样做并修复了错误,所以这里是正确的代码:

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

        }
    }

已针对此问题在 Open Radar 上提交错误报告 https://openradar.appspot.com/32939029

记者提供了几个解决方法:

  1. Not using a spring for the timing parameters
  2. Putting the layoutIfNeeded call in the keyframe animation as well
  3. Add an extension on CAKeyframeAnimation where you define a fromValue and ignore it being set

第三种解决方案不太可能影响您的动画。您可以在 Swift...

中添加 CAKeyframeAnimation 扩展名
extension CAKeyframeAnimation {

    @objc func setFromValue(_ value: Any?) {
    }
}