Commit ed4a5637 authored by toby's avatar toby

background animations

parent 061140da
......@@ -8,6 +8,12 @@
import UIKit
struct BGTransition {
let duration: Double
let startColor: UIColor
let endColor: UIColor
}
func degreesToRadians(degrees: CGFloat) -> CGFloat {
return degrees * (3.1415926 / 180)
}
......@@ -18,21 +24,36 @@ func CGRectFromOrigin(view: UIView, xPercent: CGFloat, yPercent: CGFloat, width:
return CGRect(x: x, y: y, width: width, height: height)
}
func randPercent() -> CGFloat {
func randomPercent() -> CGFloat {
return CGFloat(Float(arc4random()) / Float(UINT32_MAX))
}
func randomColor() -> UIColor {
return UIColor.init(red: randomPercent(), green: randomPercent(), blue: randomPercent(), alpha: 1)
}
func randomTransition() -> BGTransition {
let duration = Double((arc4random() % 5) + 3)
var start = randomColor()
var end = randomColor()
return BGTransition(duration: duration, startColor: start, endColor: end)
}
class ViewController: UIViewController {
var animator: UIDynamicAnimator!
var gravity: UIGravityBehavior!
var collision: UICollisionBehavior!
var touch: UIPanGestureRecognizer!
var backgroundTransition: BGTransition!
override func viewDidLoad() {
super.viewDidLoad()
backgroundTransition = randomTransition()
view.backgroundColor = backgroundTransition.startColor
let barrier = UIView(frame: CGRectFromOrigin(view, 0, 40, 130, 20))
barrier.backgroundColor = UIColor.redColor()
barrier.backgroundColor = randomColor()
view.addSubview(barrier)
gravity = UIGravityBehavior()
......@@ -47,6 +68,10 @@ class ViewController: UIViewController {
animator.addBehavior(gravity)
animator.addBehavior(collision)
let hold = UILongPressGestureRecognizer(target: self, action: Selector("backgroundHeld:"))
view.addGestureRecognizer(hold)
}
override func didReceiveMemoryWarning() {
......@@ -54,24 +79,45 @@ class ViewController: UIViewController {
// Dispose of any resources that can be recreated.
}
@IBAction func newBlock(sender : AnyObject) {
@IBAction func backgroundHeld(hold: UILongPressGestureRecognizer) {
switch hold.state {
case UIGestureRecognizerState.Began:
println("started bro")
UIView.animateWithDuration(
self.backgroundTransition.duration,
delay: 0.0,
options: UIViewAnimationOptions.CurveEaseOut,
animations: {
self.view!.backgroundColor = self.backgroundTransition.endColor
},
completion: {
(finished: Bool) -> Void in
println("done animating bro")
})
case UIGestureRecognizerState.Ended:
println("ended bro")
default:
println("fuck you bro" )
}
}
@IBAction func newBlock(sender: AnyObject) {
let location = sender.locationInView(view)
let block: UIView
block = UIView(frame: CGRect(x: location.x, y: location.y, width: 60, height: 60))
block.center = location
block.backgroundColor = UIColor.init(red: randPercent(), green: randPercent(), blue: randPercent(), alpha: 1)
block.backgroundColor = UIColor.init(red: randomPercent(), green: randomPercent(), blue: randomPercent(), alpha: 1)
view.addSubview(block)
gravity.addItem(block)
collision.addItem(block)
let touch = UITapGestureRecognizer(target: self, action: Selector("blockTouch:"))
block.addGestureRecognizer(touch)
let tap = UITapGestureRecognizer(target: self, action: Selector("blockTouch:"))
block.addGestureRecognizer(tap)
}
@IBAction func blockTouch(recognizer: UIGestureRecognizer) {
println("block touched!")
recognizer.view!.removeFromSuperview()
gravity.removeItem(recognizer.view!)
collision.removeItem(recognizer.view!)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment