【问题标题】:How to Stop a Button Animating by conditions如何按条件停止按钮动画
【发布时间】:2017-12-22 14:48:58
【问题描述】:

我有一个函数可以让某个按钮淡入淡出。我在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()
        })
    })
}

【问题讨论】:

  • 您是否尝试将其设置为 false? .setAnimationsEnabled(启用:布尔)
  • 那不行。我认为问题是动画在重复?我是 swift 新手,所以我不知道从哪里开始创建一个停止这种情况的函数?

标签: swift animation uibutton removeall


【解决方案1】:

更改完成块以检查completed 参数。 completedtrue 动画完成时(即未取消时)。只有当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()

【讨论】:

  • 还是不行?我将条件设置为if Voutput.text != ""{ self.EnterDet.layer.removeAllAnimations() } else { animateText() },动画仍然不会关闭?
  • 你把代码放在哪里了?您是否添加了 both if completed 块?
  • 按钮将我带到输入窗口。是segue吗?
  • 输入窗口是在同一个视图控制器中弹出的
  • 当你认为viewDidAppear 被调用时,你确定它被调用了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多