2017-03-17 8 views
4

...パーフェクトSwift3ボイン

@IBOutlet var barHeight: NSLayoutConstraint! 


    barHeight.constant = barShut?30:100 
    self.view.layoutIfNeeded() 
    t = !barShut?30:100 

    UIView.animate(withDuration: 0.15, 
     delay: 0, 
     options: UIViewAnimationOptions.curveEaseOut, 

     animations: {() -> Void in 
      self.barHeight.constant = t 
      self.view.layoutIfNeeded() 
     }, 

     completion: {_ in 
      Screen.barShut = !Screen.barShut 
     } 
    ) 

素晴らしいことだ...

enter image description here

しかし、どのようにそれはこのようボインになるだろうか?

enter image description here

(私はこれを行うには知っているだろう唯一の方法は、春の減衰のための数行のコードで、CADisplayLinkを使用し、である。)のUIKitでこの利用可能ですか?

+0

あなたは[アニメーションの長さ:遅延:ダンピングを伴うスプリングの使用:初期のスプリング速度:オプション:アニメーション:完了:](https:// developer.apple.com/reference/uikit/uiview/1622594-animatewithduration)? – Paulw11

+0

他の変更を制約の 'constant'にアニメーション化するのと同じように、アニメーションを作成します - 私の答えを見てください。 – Paulw11

答えて

1

あなたはUIViewのに組み込まれているspring animation methodを使用することができます。

func toggleBar() -> Void { 
    self.view.layoutIfNeeded() 
    let newHeight:CGFloat = !barShut ? 30:100 
    barShut = !barShut 

    barHeightConstraint.constant = newHeight 

    UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 3, options: [], animations: { 
     self.view.layoutIfNeeded() 
    }, completion: nil) 

} 

あなたは現実的に見えるするバウンスのためのために、第2の0.15よりも長いアニメーションの再生時間をお勧めします。私の価値観はかなり良いと思いますが、あなたは彼らと一緒に遊んで、あなたが行っている正確な効果を得ることができます。

アニメーションの時間が長いため、以前のアニメーションがまだ実行されている間に、開いた/終了したボタンをタップできます。完了ブロックにbarShutを設定すると、バーはすべてのタップに反応しませんでした。これに対処するために、アニメーションの外にトグルを移動しました。

+0

ああ、幻想的な、春アニメーションが組み込まれています! – Fattie

+0

誰でも読むのはちょうどBTWなので、便利な '.beginFromCurrentState'は時には最終的なparaでPaulが言及している問題に関して役立つことがあります。 – Fattie

+0

あなたの賞金のように見えるポール - ありがとう! :) – Fattie