Skip to content

Instantly share code, notes, and snippets.

@oaleeapp
Created January 20, 2017 19:05
Show Gist options
  • Select an option

  • Save oaleeapp/53d3f5fbffb1951d9549fcb4b66dbc65 to your computer and use it in GitHub Desktop.

Select an option

Save oaleeapp/53d3f5fbffb1951d9549fcb4b66dbc65 to your computer and use it in GitHub Desktop.

Revisions

  1. oaleeapp created this gist Jan 20, 2017.
    79 changes: 79 additions & 0 deletions DynamicWorld
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@

    import UIKit
    import PlaygroundSupport



    class physicViewController : UIViewController {


    lazy var dynamicAnimator: UIDynamicAnimator = {

    return UIDynamicAnimator(referenceView: self.view)
    }()

    lazy var cube: UIView = {

    let cube = UIView(frame: CGRect(origin: CGPoint(x: 100.0, y: 100.0), size: CGSize(width: 100.0, height: 100.0)))
    cube.backgroundColor = UIColor.blue

    self.view.addSubview(cube)

    return cube

    }()

    let gravity = UIGravityBehavior()
    let collision = UICollisionBehavior()
    let itemBehavior = UIDynamicItemBehavior()

    override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.white


    }

    override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)




    gravity.addItem(cube)
    dynamicAnimator.addBehavior(gravity)

    collision.addItem(cube)
    collision.translatesReferenceBoundsIntoBoundary = true

    dynamicAnimator.addBehavior(collision)

    itemBehavior.addItem(cube)
    itemBehavior.allowsRotation = true
    itemBehavior.friction = 0.3
    itemBehavior.angularResistance = 0.1
    dynamicAnimator.addBehavior(itemBehavior)

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(pushIt))
    view.addGestureRecognizer(tapGesture)


    }

    func pushIt(){
    let push = UIPushBehavior(items: gravity.items, mode: .instantaneous)
    push.magnitude = 10.0
    push.pushDirection = CGVector.init(dx: 5.0, dy: -10.0)

    dynamicAnimator.addBehavior(push)
    }


    }



    let viewController = physicViewController()


    PlaygroundPage.current.liveView = viewController