2016-08-04 3 views
0

テキストとボタンの単純なアニメーションを画面の中央から画面の中央にかけて作成しましたが、そうではありません。画面から画面の外に出ます。基本的に、アニメーションは逆になります。Swift:animateWithDurationの動作

私は他のアプリが同じコードで動かない方法で動作するのを見たので、何が間違っているのかわかりません。景色が開始場所について

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var welcomeText: UILabel! 
@IBOutlet weak var worksText: UILabel! 
@IBOutlet weak var loginButton: UIButton! 
@IBOutlet weak var signUp: UIButton! 
@IBOutlet weak var socialLog: UILabel! 

override func viewDidLoad() { 
    super.viewDidLoad() 

} 

override func viewWillAppear(animated: Bool) { 

    welcomeText.alpha = 0.0 
    worksText.alpha = 0.0 
    loginButton.alpha = 0.0 
    signUp.alpha = 0.0 
    socialLog.alpha = 0.0 

    welcomeText.center.x -= view.bounds.width 
    worksText.center.x -= view.bounds.width 
    socialLog.center.x -= view.bounds.width 
    signUp.center.x -= view.bounds.width 
} 

override func viewDidAppear(animated: Bool) { 

    self.welcomeText.fadeIn() 
    UIView.animateWithDuration(0.7, delay: 0.1, options: .CurveEaseOut, animations: { 
     self.welcomeText.center.x += self.view.bounds.width 
     self.view.layoutIfNeeded() 
     }, completion: nil) 

    self.worksText.fadeIn() 
    UIView.animateWithDuration(0.7, delay: 0.5, options: .CurveEaseOut, animations: { 
     self.worksText.center.x += self.view.bounds.width 
     self.view.layoutIfNeeded() 
     }, completion: nil) 

    self.socialLog.fadeIn() 
    UIView.animateWithDuration(0.7, delay: 0.3, options: .CurveEaseOut, animations: { 
     self.socialLog.center.x += self.view.bounds.width 
     self.view.layoutIfNeeded() 
     }, completion: nil) 

    self.signUp.fadeIn() 
    UIView.animateWithDuration(0.7, delay: 0.7, options: .CurveEaseOut, animations: { 
     self.signUp.center.x += self.view.bounds.width 
     self.signUp.alpha = 0.5 
     self.view.layoutIfNeeded() 
     }, completion: nil) 
} 
} 

extension UIView { 
func fadeIn(duration: NSTimeInterval = 0.5, delay: NSTimeInterval = 0.0, completion: ((Bool) -> Void) = {(finished: Bool) -> Void in}) { 
    UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: { 
     self.alpha = 1.0 
     }, completion: completion) } 

func fadeOut(duration: NSTimeInterval = 0.5, delay: NSTimeInterval = 0.0, completion: (Bool) -> Void = {(finished: Bool) -> Void in}) { 
    UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: { 
     self.alpha = 0.0 
     }, completion: completion) 
} 
} 
+0

自動レイアウトを使用していますか?もしそうなら、それはおそらくあなたの問題です。あなたはlayoutIfNeeded()を呼び出していますが、制約を直接編集しているわけではありません。 – TheValyreanGroup

答えて

2

情報これをデバッグするために必要とされるであろうが、私は強く、+ =または使用しないことをお勧めします - として、このようなアニメーションのために=を::(ここ

は、コードがあります。あなたは、それが現在の状態に完全に依存するため動作が予測できないですが、経験している

は代わりに、あなたはviewWillAppearでこれを行うことができます:。*view*.center.x = -(*view*.frame.width/2) とviewDidAppearで:ビュー .center.x = どのような値を、あなたは彼を必要とするre

さらに、これまでのすべてのアプローチでは、フレームではなく、制約を使用してアニメートする方法があります。

+0

ありがとうございます。制約アニメーションを使用します。私は今それを試してみよう、より一般的なプラクティスと使用の場合、私は制約のアニメーションを使用します。ありがとう。 –