removeAllAnimations() 后重置动画
Reset animation after removeAllAnimations()
在社区的大力帮助下,我制作了按钮图像的放大和缩小动画。问题是,在第一个 运行 之后按钮被禁用并且在应用程序重新启动之前不会再次工作。我想要的是单击按钮并完成动画后,它会回到起点,您可以再次单击它。这里有人可以帮助我吗?
@IBAction func coffeeButton(sender: UIButton) {
var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "stopButtonAnimation", userInfo: nil, repeats: false)
let options = UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.Repeat | UIViewAnimationOptions.CurveEaseInOut
UIView.animateWithDuration(0.5, delay: 0, options: options, animations: {
self.button.transform = CGAffineTransformMakeScale(0.5, 0.5)
}, completion: nil)
}
func stopButtonAnimation(){
button.layer.removeAllAnimations()
}
更正了您的 stopButtonAnimation 函数。
应用变换的作用是将按钮的比例设置为其原始值,这样您就可以再次看到动画。
func stopButtonAnimation(){
button.layer.removeAllAnimations()
button.layer.transform = CATransform3DIdentity
}
您的原始代码中的动画在缩放值为 0.5 时停止,因此下次您单击该按钮时您将看不到它(因为它一直在从 0.5 到 0.5 的动画缩放)。
在社区的大力帮助下,我制作了按钮图像的放大和缩小动画。问题是,在第一个 运行 之后按钮被禁用并且在应用程序重新启动之前不会再次工作。我想要的是单击按钮并完成动画后,它会回到起点,您可以再次单击它。这里有人可以帮助我吗?
@IBAction func coffeeButton(sender: UIButton) {
var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "stopButtonAnimation", userInfo: nil, repeats: false)
let options = UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.Repeat | UIViewAnimationOptions.CurveEaseInOut
UIView.animateWithDuration(0.5, delay: 0, options: options, animations: {
self.button.transform = CGAffineTransformMakeScale(0.5, 0.5)
}, completion: nil)
}
func stopButtonAnimation(){
button.layer.removeAllAnimations()
}
更正了您的 stopButtonAnimation 函数。
应用变换的作用是将按钮的比例设置为其原始值,这样您就可以再次看到动画。
func stopButtonAnimation(){
button.layer.removeAllAnimations()
button.layer.transform = CATransform3DIdentity
}
您的原始代码中的动画在缩放值为 0.5 时停止,因此下次您单击该按钮时您将看不到它(因为它一直在从 0.5 到 0.5 的动画缩放)。