我正在尝试使用keyframe为按钮设置动画,并为我想要修改其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'
最佳答案
已针对此问题https://openradar.appspot.com/32939029在Open Radar上提交了一个错误报告。
记者提供了一些解决方法:
layoutIfNeeded
调用放入关键帧动画中CAKeyframeAnimation
上添加扩展名,您可以在其中定义fromValue
,而忽略正在设置的第三种解决方案不太可能影响您的动画。您可以在Swift中添加
CAKeyframeAnimation
扩展...extension CAKeyframeAnimation {
@objc func setFromValue(_ value: Any?) {
}
}