2016-11-08 47 views
0

我遇到了一些困難動畫我的觀點的layoutConstraints之一:UIView的動畫而不考慮時間瞬間完成或延緩

UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: { 
       self.topLayoutConstraint?.constant = self.currentYPosition 
       self.layoutIfNeeded() 
       }, completion: nil) 

無論我使用的持續時間或延遲,動畫瞬間完成(我的觀點從之前的位置跳到新的位置)。

我檢查了所有的值,他們是正確的。 我也檢查過,並調用動畫和完成。

任何想法?

編輯:

@objc private func viewDragged(_ recognizer: UIPanGestureRecognizer) { 

     let location = recognizer.location(in: self.superview) 

     switch recognizer.state { 
     case .ended: 

      if (recognizer.direction == .Down) {   // Collapsing the slide panel 

       currentYPosition = parentViewHeight - minimumVisibleHeight - statusBarHeight - navBarHeight 
      } 
      else if (recognizer.direction == .Up) {  // Expanding the slide panel 

       // Substracting `minimumVisibleHeight` so that the dragable part is hidden behind the navigation bar 
       currentYPosition = -minimumVisibleHeight 
      } 

      self.topLayoutConstraint?.constant = self.currentYPosition 
      UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: { 
       self.layoutIfNeeded() 
       }, completion: nil) 
      break 
     case .began: 
      HomeSlidePanelContainerView.draggingPreviousPosition = location.y 
     default: 
      self.layoutIfNeeded() 

      if (location.y < (parentViewHeight - minimumVisibleHeight - statusBarHeight - navBarHeight) 
       || location.y > (statusBarHeight + navBarHeight)) { 

       currentYPosition -= HomeSlidePanelContainerView.draggingPreviousPosition - location.y 
       topLayoutConstraint?.constant = currentYPosition 
       self.layoutIfNeeded() 
      } 
      HomeSlidePanelContainerView.draggingPreviousPosition = location.y 
      break 
     } 

    } 
+0

您是否嘗試在'layoutIfNeeded()'之前調用'setNeedsUpdateConstraints()'? – werediver

+0

@werediver喜歡在動畫還是之前?在這兩種情況下,nop我沒有 – Moucheg

+0

把這行'self.topLayoutConstraint?.constant = self.currentYPosition'移出動畫塊。 – holex

回答

0

我已經設法找到解決方案。這可能會幫助其他人,所以我正在回答我自己的問題。

我所做的一切是這樣的:

self.topLayoutConstraint?.constant = self.currentYPosition 

UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {    
       self.layoutIfNeeded() 
}, completion: nil) 

正如你所看到的,我改變了一個值,topLayoutConstraint,這是self約束這是一個UIView

然後,我試圖通過在動畫關閉中調用self.layoutIfNeeded來動畫化該更改。

這裏的事情是layoutIfNeeded()適用於子視圖,而不是視圖本身。所以在我的情況下,我試圖動畫化self的子視圖的佈局,而不是self本身。

更改self.layoutIfNeeded()self.superview?.layoutIfNeeded()和Voilà!

0

設置動畫塊前的約束:

self.topLayoutConstraint?.constant = self.currentYPosition 

UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: {    
       self.layoutIfNeeded() 
}, completion: nil) 
+0

已經嘗試過,但結果相同:/ – Moucheg

+0

我假設錯誤位於其他位置。也許發佈更多的代碼。 – shallowThought

+0

剛剛用更多的代碼編輯我的問題! – Moucheg

0

我經常使用Snapkit或磚石做自動佈局,也許你可以嘗試it.It是熟悉蘋果例如我今天使用的子代碼:

UIView.animate(withDuration: 1.0, animations: { 
minLabel.snp.updateConstraints { (make) in 
      make.top.equalTo(view.snp.centerY) 
        } 
}) 
self.view.layoutIfNeeded() 
+0

真的不想包括一個只做一個動畫的pod:/但我同意,這是一個有趣的lib – Moucheg

+0

如果沒有編譯器錯誤,並且刷新視圖時,UI有時是正確的,但有時候不是。它可能是線程問題。獲取動畫和自動佈局同步。 – Haixiao