InterFace Builder の AutoLayout で下記の画像のようにUILabelを画面の中心にレイアウトしました。
そして、下記のコードのようにAnimationさせました。
ViewController.swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 // アニメーションさせるUILabel 6 // 画面の中央に表示されるようにIBでAutoLayoutを設定しています 7 @IBOutlet weak var animationLabel: UILabel! 8 9 // animationLabelのY座標の制約 10 @IBOutlet weak var animationLabelCenterYConstraint: NSLayoutConstraint? 11 // 上の制約は下記の内容になります 12 // NSLayoutConstraint( 13 // item: animationLabel, 14 // attribute: .CenterY, 15 // relatedBy: .Equal, 16 // toItem: self.view, 17 // attribute: .CenterY, 18 // multiplier: 1, 19 // constant: 0 20 // ) 21 22 override func viewDidAppear(animated: Bool) { 23 super.viewDidAppear(animated) 24 25 // 制約を外します 26 if let constraint = animationLabelCenterYConstraint { 27 self.view.removeConstraint(constraint) 28 } 29 30 // 新しい制約を作成します 31 let newConstraint = NSLayoutConstraint( 32 item: animationLabel, 33 attribute: .CenterY, 34 relatedBy: .Equal, 35 toItem: self.view, 36 attribute: .CenterY, 37 multiplier: 0.5, 38 constant: 0 39 ) 40 // 新しい制約では multiplierを2に設定します 41 42 // 新しい制約を追加します 43 self.view.addConstraint(newConstraint) 44 45 // アニメーションを実行します 46 UIView.animateWithDuration(5) { () -> Void in 47 self.view.layoutIfNeeded() 48 } 49 } 50}
上記のコードでアニメーションはするのですが、IBで制約を設定していたりコードで制約を設定していたりして、わかりにくいコードになってしまいます。
できれば、IBでアニメーション前と後のAutoLayoutを設定したいのですが可能でしょうか。
コードでは、アニメーションの実行のみを行えればいいなと思っております。
わかりにくい点などありましたら、コメントでいただけると幸いです。
よろしくお願いいたします。
追記:下記サイトにて、AutoLayoutのPriorityを操作してAnimationを行う方法がありました。下記の方法が今の段階ではベターな方法なのでしょうか。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/21 13:12