gliyao
10/31/2014 - 1:02 AM

constrainAnimation

//
//  ViewController.swift
//  constrainAnimation
//
//  Created by Liyao on 2014/10/30.
//  Copyright (c) 2014年 swiftTaipei. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    // Not necessary if you use constrain animation
    @IBOutlet weak var boxView: UIView!
    
    // 1. Connect constrain with IBOutlet
    @IBOutlet weak var boxBottomSpace: NSLayoutConstraint!
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // Pick a animation you want
        
        fallingAnimation()
//        fallAndfloatAnimation()
//        move()
    }
    
    
    // MARK: Animation by frame
    func move()
    {
        // Calculate everything
        let screenSize = UIScreen.mainScreen().bounds
        let boxFrame = boxView.frame
        var destBoxFrame: CGRect = boxView.frame
        destBoxFrame.origin.y = CGRectGetHeight(screenSize) - CGRectGetHeight(boxView.frame)
        destBoxFrame.origin.x = CGRectGetWidth(screenSize) - CGRectGetWidth(boxView.frame)
        
        UIView.animateWithDuration(1, animations: {
            self.boxView.frame = destBoxFrame
        })
    }
    
    // MARK: Animation by constrain
    func fallingAnimation()
    {
        // 2. Change constrain priority
        boxBottomSpace.priority = 750
        
        // 3. OS will calculate animation for you
        UIView.animateWithDuration(1, animations: {
            self.view.layoutIfNeeded()
        })
    }
    
    func fallAndfloatAnimation()
    {
        boxBottomSpace.priority = 750
        
        // Falling animation
        UIView.animateWithDuration(2, animations: {
            self.view.layoutIfNeeded()
            }, completion: {finish in
                
                // Move up animation
                self.boxBottomSpace.priority = 250
                UIView.animateWithDuration(3, animations: {
                    self.view.layoutIfNeeded()
                })
        })
    }
}