如何根据条件停止按钮动画
How to Stop a Button Animating by conditions
我有一个函数可以让某个按钮动画淡入和淡出。我在 viewDidLoad
中启动了动画,所以一旦我打开应用程序,动画就会开始工作。该按钮将我带到输入 window,当输入字段有输入时,我希望动画停止。我尝试从有条件的文本中删除动画,但即使满足条件,动画也会保持动画效果。
我如何创建一个删除此动画的函数?
这是我的代码:
func animateText(){
UIView.animate(withDuration: 0.5, animations: {
self.EnterDet.alpha = 1
}, completion: {
(Comnpleted : Bool) -> Void in
UIView.animate(withDuration: 0.5, delay: 1.5, options: UIViewAnimationOptions.allowUserInteraction, animations: {
self.EnterDet.alpha = 0.1
}, completion: {
(Completed : Bool) -> Void in
self.animateText()
})
})
}
更改完成块以检查 completed
参数。 completed
是 true
动画完成时(即未取消时)。只有当completed
为true
时才调用下一步动画
UIView.animate(withDuration: 0.5, animations: {
self.EnterDet.alpha = 1
}, completion: {
(completed : Bool) -> Void in
if completed {
UIView.animate(withDuration: 0.5, delay: 1.5, options: UIViewAnimationOptions.allowUserInteraction, animations: {
self.EnterDet.alpha = 0.1
}, completion: {
(completed : Bool) -> Void in
if completed {
self.animateText()
} else {
self.EnterDet.alpha = 1
}
})
}
})
然后,当你想结束动画时,调用:
self.EnterDet.layer.removeAllAnimations()
我有一个函数可以让某个按钮动画淡入和淡出。我在 viewDidLoad
中启动了动画,所以一旦我打开应用程序,动画就会开始工作。该按钮将我带到输入 window,当输入字段有输入时,我希望动画停止。我尝试从有条件的文本中删除动画,但即使满足条件,动画也会保持动画效果。
我如何创建一个删除此动画的函数?
这是我的代码:
func animateText(){
UIView.animate(withDuration: 0.5, animations: {
self.EnterDet.alpha = 1
}, completion: {
(Comnpleted : Bool) -> Void in
UIView.animate(withDuration: 0.5, delay: 1.5, options: UIViewAnimationOptions.allowUserInteraction, animations: {
self.EnterDet.alpha = 0.1
}, completion: {
(Completed : Bool) -> Void in
self.animateText()
})
})
}
更改完成块以检查 completed
参数。 completed
是 true
动画完成时(即未取消时)。只有当completed
为true
时才调用下一步动画
UIView.animate(withDuration: 0.5, animations: {
self.EnterDet.alpha = 1
}, completion: {
(completed : Bool) -> Void in
if completed {
UIView.animate(withDuration: 0.5, delay: 1.5, options: UIViewAnimationOptions.allowUserInteraction, animations: {
self.EnterDet.alpha = 0.1
}, completion: {
(completed : Bool) -> Void in
if completed {
self.animateText()
} else {
self.EnterDet.alpha = 1
}
})
}
})
然后,当你想结束动画时,调用:
self.EnterDet.layer.removeAllAnimations()