gpt4 book ai didi

swift - 如何根据条件停止按钮动画

转载 作者:行者123 更新时间:2023-11-28 15:26:30 30 4
gpt4 key购买 nike

我有一个函数可以让某个按钮动画淡入和淡出。我在 viewDidLoad 中启动动画,所以一旦我打开应用程序,动画就会开始工作。该按钮将我带到一个输入窗口,当输入字段有输入时,我希望动画停止。我尝试从有条件的文本中删除动画,但即使满足条件,动画也会保持动画效果。我如何创建删除此动画的函数?

这是我的代码:

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()
})
})
}

最佳答案

更改完成 block 以检查 completed 参数。 completed 在动画完成时为 true(即未取消时)。只有当 completedtrue 时,您才会调用动画的下一步。

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()

关于swift - 如何根据条件停止按钮动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45154849/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com