2016-07-18 6 views
0

どのようにして特定のCGPointに行くためにプログラムで作成されたオブジェクトをアニメーション化できますか? CGAffineTransformMakeTranslationを使用しないでください。移動する方法だけであり、移動する場所ではありません。私が試してみましたSwift、特定のCGPointにアニメーションを適用する

:translatesAutoresizingMaskIntoConstraints =

真は、しかし、それは違いはありません。

私はこれを試してみました:シミュレータで実行したときに

//Define screen sizes 
let screenSize: CGRect = UIScreen.mainScreen().bounds 
let screenWidth = screenSize.width 
let screenHeight = screenSize.height 

// Sizes of centre cards 
let centreCardWidth = screenWidth/5 
let centreCardHeight = (screenWidth/5)/0.625 
let centralizeViewHorizontalCard = (screenWidth/2) - ((screenWidth/5)/2) 
let centralizeViewVerticleCard = screenHeight/2 

UIView.animateWithDuration(0.25, delay: 0.5, options: [], animations: { 
    centreCard3.frame = CGRectMake(centralizeViewHorizontalCard, centralizeViewVerticleCard, centreCard3.frame.size.width, centreCard3.frame.size.height) 
    }, completion: nil) 

しかしには、絶対に何も起こりませんでした。

+0

を初期化していますか? – Chajmz

+0

いつこのコードを呼び出していますか? – jrturton

+0

でViewDidLoad。 – RufusV

答えて

2

アニメーションを作成するときにUIがレンダリングされていません。 viewDidAppearに配置します。これは私の作品:

class ViewController: UIViewController { 

    @IBOutlet weak var centreCard3: UIView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func viewDidAppear(animated: Bool) { 
     let screenSize: CGRect = UIScreen.mainScreen().bounds 
     let screenWidth = screenSize.width 
     let screenHeight = screenSize.height 

     // Sizes of centre cards 

     let centralizeViewHorizontalCard = (screenWidth/2) - ((screenWidth/5)/2) 
     let centralizeViewVerticleCard = screenHeight/2 

     UIView.animateWithDuration(0.5, delay: 0.5, options: [], animations: { 
      self.centreCard3.frame = CGRectMake(centralizeViewHorizontalCard, centralizeViewVerticleCard, self.centreCard3.frame.size.width, self.centreCard3.frame.size.height) 
      }, completion: nil) 
    } 
} 

結果:あなたのcentreCard3が

Result

関連する問題