Snippets

Dominik swiftAnimation

Created by Dominik last modified
//
//  ViewController.swift
//  animationTest
//
//  Created by dominik on 21/12/2015.
//  Copyright © 2015 cookingcode. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var myButton: UIButton!
    
    var myRedSquare = UIView()
    var myGreenSquare = UIView()
    var myLabel = UILabel()
    var myText = UILabel()
    var myWhiteSquare = UIView()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        myRedSquare = UIView(frame: CGRectMake(100, 100, 240, 240))
        myRedSquare.backgroundColor = UIColor.redColor()
        self.myRedSquare.alpha = 0
        self.view.addSubview(myRedSquare)
        
        myGreenSquare = UIView(frame: CGRectMake(100, 100, 240, 240))
        myGreenSquare.backgroundColor = UIColor.greenColor()
        self.view.addSubview(myGreenSquare)
        
        myLabel = UILabel(frame: CGRectMake(100, 100, 100, 500))
        myLabel.textColor = UIColor.blackColor()
        myLabel.text = "Hello World!"
        self.view.addSubview(myLabel)
        
        myText = UILabel(frame: CGRectMake(100, 100, 100, 600))
        myText.textColor = UIColor.blackColor()
        myText.text = "my text!"
        myText.alpha = 1
        self.view.addSubview(myText)
        
        myWhiteSquare = UIView(frame: CGRectMake(100, 340, 100, 30))
        myWhiteSquare.backgroundColor = UIColor.whiteColor()
        myWhiteSquare.alpha = 0
        self.view.addSubview(myWhiteSquare)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func myButtonPressed(sender: AnyObject) {
        if self.myGreenSquare.alpha == 1{
            UIView.animateWithDuration(0.3, animations: {
                self.myGreenSquare.alpha = 0
            })
            UIView.animateWithDuration(0.3, animations: {
                self.myRedSquare.alpha = 1
            })
        }else{
            UIView.animateWithDuration(0.3, animations: {
                self.myGreenSquare.alpha = 1
            })
            UIView.animateWithDuration(0.3, animations: {
                self.myRedSquare.alpha = 0
            })
        }
        
        UIView.animateWithDuration(0.3, animations: {
            self.myWhiteSquare.alpha = 1
            self.myText.alpha = 0
        })
        UIView.animateWithDuration(0.3, delay: 0.3, options: [], animations: {
            self.myWhiteSquare.alpha = 0
            self.myText.alpha = 1
            if self.myLabel.text == "Hello World!"{
                self.myLabel.text = "Hello!"
            }else{
                self.myLabel.text = "Hello World!"
            }
            
            }, completion: nil)
    }

}

Comments (0)