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