2016-11-08 6 views
0

は私が私の見解のlayoutConstraintsのいずれかをアニメーションいくつかの困難を抱えている遅らせている:私は使用期間または遅延は、アニメーションが即座に行われているものは何でもUIViewのアニメーションが期間に関してせずに即座に行うか

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

(私の意見は以前の位置から新しい位置にジャンプする)。

私はすべての値をチェックしており、それは正しいです。 私もチェックして、アニメーションと補完が呼び出されました。

アイデア?

EDIT:アニメーションブロックの前に制約を設定し

@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を変更しています、それは、UIViewあるself上の制約です。

アニメーションの終了時にself.layoutIfNeededを呼び出して、その変更をアニメーション化しようとしています。

layoutIfNeeded()は、ビュー自体ではなくサブビューに適用されます。だから私の場合はselfの代わりにselfのサブビューのレイアウトをアニメートしようとしていました。

変更されましたself.layoutIfNeeded()self.superview?.layoutIfNeeded()とVoilà!!

0

:私は、多くの場合、Snapkitや石工が自動レイアウトを行う使用

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

、多分あなたはit.Itを試すことができますAppleのに精通していますautolayout、それは最高です!私は今日使用しているサブコード例:

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

ただ1つのアニメーションを行うために実際にポッドを入れたくありません:/私は同意します、興味深いlibです – Moucheg

+0

コンパイラエラーがなく、ビューをリフレッシュすると、いつかUIが正しくなることはありません。おそらくスレッドの問題です。アニメーションと自動レイアウトを同期させてください。 – Haixiao

関連する問題