Created
October 9, 2014 01:04
-
-
Save rnagella/61e897b5510be67a1468 to your computer and use it in GitHub Desktop.
Bouncing Labels using swift - Animation, physics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// Bouncing Labels | |
// | |
// Created by Ranjith Nagella on 10/6/14. | |
// Copyright (c) 2014 Ranjith Nagella. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
// create ivars | |
var firstLabel: UILabel! | |
var iPhoneLable: UILabel! | |
var myView: UIView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
firstLabel = UILabel() | |
iPhoneLable = UILabel() | |
addLabels() | |
// myView = UIView() | |
// myView.backgroundColor = UIColor.yellowColor() | |
// myView.center = CGPoint(x: 0, y:0) | |
// Where is the (0,0) coordinate in terms of the iPhone screen | |
// The origin is specified at top left corner and extends x and y towards down and right corners. | |
// myView.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height) | |
// view.addSubview(myView) | |
view.backgroundColor = UIColor.yellowColor() | |
println(view.bounds.size.height) // 667.0 | |
println(view.bounds.size.width) // 375.0 | |
view.backgroundColor = UIColor.redColor() | |
// How bing is the iPhone screen in pixels | |
// It's 667 px interms of height and 375px interms of width | |
view.bounds = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height) | |
// view.bounds.width = view.bounds.size.width | |
var tapGesture = UITapGestureRecognizer(target: self, action: Selector("handelGesture:")) | |
view.addGestureRecognizer(tapGesture) | |
} | |
func handelGesture(tapGesture: UITapGestureRecognizer) { | |
// println("Tap") | |
addLabels() | |
} | |
var springness = CGFloat(0.9) | |
func addLabels() { | |
firstLabel.text = "My First" | |
firstLabel.font = UIFont.systemFontOfSize(36) | |
firstLabel.sizeToFit() | |
firstLabel.center = CGPoint(x: 100, y: 40) | |
view.addSubview(firstLabel) | |
UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: springness, initialSpringVelocity: 0.0, options: nil, animations: { | |
self.firstLabel.center = CGPoint(x: 100, y: 40 + 100); | |
if (self.springness >= 0.1) { | |
println("true") | |
//println(CGFloat(self.springness) >= CGFloat(0.1)) | |
println(self.springness) | |
self.springness = CGFloat(self.springness - 0.1) | |
} else { | |
println("entered else") | |
self.springness = CGFloat(0.9) | |
} | |
}, completion: nil) | |
iPhoneLable.center = CGPoint(x: 280, y: 180) | |
iPhoneLable.text = "iPhone App" | |
iPhoneLable.font = UIFont.boldSystemFontOfSize(36) | |
iPhoneLable.sizeToFit() | |
iPhoneLable.alpha = 0 | |
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.TransitionFlipFromRight, animations: { | |
self.iPhoneLable.center = CGPoint(x: 160, y: 180) | |
self.iPhoneLable.alpha = 1 | |
}, completion: nil) | |
view.addSubview(iPhoneLable) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment