2017-07-17 40 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 –