2017-07-17 58 views
0

我有一个函数可以将某个按钮设置为淡入和淡出。我在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() 
     }) 
    }) 
} 
+0

你尝试此设置为false? .setAnimationsEnabled(启用:布尔) – brw59

+0

那不行。我认为问题在于动画重复?即时通讯新的,所以我不知道从哪里开始创建一个功能,停止这一点? –

回答

1

更改完成块检查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() 
+0

仍然没有工作?我把条件设置为'if Voutput.text!=“”self.EnterDet.layer.removeAllAnimations() } else {animateText()}'并且动画仍然不会关闭? –

+0

你把这些代码放在哪里?你是否添加*两个*'如果完成'块? – vacawama

+0

条件代码在ViewDidAppear –