Created
September 30, 2015 18:36
-
-
Save nevyn/a831f19d81589a99c87e to your computer and use it in GitHub Desktop.
View controller to get stuff moving around on screen :D Demo video: https://lookback.io/watch/TouJLFvYCs9Kyd9XC
This file contains hidden or 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
// | |
// GFStuffMovingAroundViewController.swift | |
// GFTest | |
// | |
// Created by Nevyn Bengtsson on 2015-09-10. | |
// Copyright © 2015 Lookback AB. All rights reserved. | |
// | |
import UIKit | |
class GFStuffMovingAroundViewController: GFAppViewController { | |
@IBOutlet var stuff: [UIView]! | |
var animator: UIDynamicAnimator! | |
var timer: NSTimer? | |
var dynamic : UIDynamicItemBehavior! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
animator = UIDynamicAnimator(referenceView: self.view) | |
let collision = UICollisionBehavior(items: stuff) | |
collision.translatesReferenceBoundsIntoBoundary = true | |
animator.addBehavior(collision) | |
dynamic = UIDynamicItemBehavior(items: stuff) | |
self.animator.addBehavior(dynamic); | |
} | |
override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) | |
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "moveSomething", userInfo: nil, repeats: true) | |
} | |
override func viewDidDisappear(animated: Bool) { | |
super.viewDidDisappear(animated) | |
timer?.invalidate() | |
timer = nil | |
} | |
@objc func moveSomething() { | |
dynamic.addLinearVelocity( | |
CGPointMake( | |
CGFloat(Int(arc4random_uniform(600))-300) + 100, | |
CGFloat(Int(arc4random_uniform(600))-300) + 100 | |
), | |
forItem: stuff[Int(arc4random_uniform(UInt32(stuff.count)))] | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment