Swiftで遊ぼう! - 313 - My Fourth Project トランザクションをアニメーションにしてみる
Swiftで遊ぼう!の古い記事-> Life-LOG OtherSide
質問 : Swiftで遊ぼう! - 252 - FaceViewプロジェクト始動 - Swiftで遊ぼう! on Hatena
Beginning iPhone Development with Swift: Exploring the iOS SDK
- 作者: David Mark,Jack Nutting,Kim Topley,Fredrik Olsson,Jeff LaMarche
- 出版社/メーカー: Apress
- 発売日: 2014/11/19
- メディア: Kindle版
- この商品を含むブログを見る
My Fourth Projectも今日で最後です。今までのコーディングで瞬時に画面切り変わります。もう少し今時風に動きを加えましょう。
このトランザクションのアニメーションですが、UIViewのメソッドとして用意されているので超簡単に利用できます。
実装の仕方を覚えておくと今後の画面切りかえに使えます。
トランザクションに使うメソッドは、「クラス・メソッド」なのでインスタンス化する必要のない便利なメソッドです。
明示的にスタートと失行を指定すればいいようです。
どの場所でスタートさせるか?
Viewを切り替えるメソッドSwitchViews()に組み込みます。
@IBAction func switchViews(sender: UIBarButtonItem) { if orangeViewController?.view.superview == nil { if orangeViewController == nil { orangeViewController = storyboard?.instantiateViewControllerWithIdentifier("Orange") as? OrangeViewController } } else if blueViewController?.view.superview == nil { if blueViewController == nil { blueViewController = storyboard?.instantiateViewControllerWithIdentifier("Blue") as? BlueViewController } } UIView.beginAnimations("View Flip", context: nil) UIView.setAnimationDuration(0.4) UIView.setAnimationCurve(.EaseInOut) if blueViewController != nil && blueViewController!.view.superview != nil { UIView.setAnimationTransition(.FlipFromRight, forView: view, cache: true) orangeViewController?.view.frame = view.frame switchViewController(from: blueViewController, to: orangeViewController) } else { UIView.setAnimationTransition(.FlipFromLeft, forView: view, cache: true) blueViewController?.view.frame = view.frame switchViewController(from: orangeViewController, to: blueViewController) } UIView.commitAnimations() }
View切りかえの一連の流れは以下のコードで実装できます
どの場所でどのように実装させればいいのか色々試してみましたが意外とコードの場所は融通が利きます。
後でコードの可読性を保つために1箇所にまとめておきます。
UIView.beginAnimations("View Flip", context: nil)
UIView.setAnimationDuration(0.4)
UIView.setAnimationCurve(.EaseInOut)
UIView.setAnimationTransition(.FlipFromRight, forView: view, cache: true)
UIView.commitAnimations()
今日はここまで。