Tuesday, April 14, 2015

Swift: Animation


import UIKit

class ViewController: UIViewController {
//    let container = UIView()
//    let redSquare = UIView()
//    let blueSquare = UIView()
let coloredSquare = UIView()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // set container frame and add to the screen
//        self.container.frame = CGRect(x: 60, y: 60, width: 200, height: 200)
//        self.view.addSubview(container)
//        
//        // set red square frame up
//        // we want the blue square to have the same position as redSquare
//        // so lets just reuse blueSquare.frame
//        self.redSquare.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
//        self.blueSquare.frame = redSquare.frame
//        
//        // set background colors
//        self.redSquare.backgroundColor = UIColor.redColor()
//        self.blueSquare.backgroundColor = UIColor.blueColor()
//        
//        // for now just add the redSquare
//        // we'll add blueSquare as part of the transition animation
//        self.container.addSubview(self.redSquare)
        
        
        let button   = UIButton.buttonWithType(UIButtonType.System) as UIButton
        button.frame = CGRectMake(300, 100, 100, 50)
        button.backgroundColor = UIColor.greenColor()
        button.setTitle("Test Button", forState: UIControlState.Normal)
        button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
        
        self.view.addSubview(button)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func buttonAction(sender:UIButton!)
    {
        println("Button tapped")
//        let views = (frontView: self.redSquare, backView: self.blueSquare)
//        
//        // set a transition style
//        let transitionOptions = UIViewAnimationOptions.TransitionFlipFromBottom
//        
//        UIView.transitionWithView(self.container, duration: 1.0, options: transitionOptions, animations: {
//            // remove the front object...
//            views.frontView.removeFromSuperview()
//            
//            // ... and add the other object
//            self.container.addSubview(views.backView)
//            
//            }, completion: { finished in
//                // any code entered here will be applied
//                // .once the animation has completed
//        })
        
        self.coloredSquare.backgroundColor = UIColor.blueColor()
        
        // set frame (position and size) of the square
        // iOS coordinate system starts at the top left of the screen
        // so this square will be at top left of screen, 50x50pt
        // CG in CGRect stands for Core Graphics
        self.coloredSquare.frame = CGRect(x: 150, y: 420, width: 150, height: 1)
        
        // finally, add the square to the screen
        self.view.addSubview(coloredSquare)

        
        
        let duration=1.0
        let delay = 0.0 // delay will be 0.0 seconds (e.g. nothing)
        let options = UIViewAnimationOptions.CurveEaseInOut
        UIView.animateWithDuration(duration, delay: delay, options: options, animations: {
            //self.coloredSquare.backgroundColor = UIColor.redColor()
            
            // for the x-position I entered 320-50 (width of screen - width of the square)
            // if you want, you could just enter 270
            // but I prefer to enter the math as a reminder of what's happenings
            self.coloredSquare.frame = CGRect(x: 150, y: 120, width: 150, height: 300)
            }, completion : { animationFinished in
            
                self.coloredSquare.removeFromSuperview()
            
            })
        
        
    }
}


No comments:

Post a Comment