Skip to content

Instantly share code, notes, and snippets.

@15458434
Last active May 6, 2016 12:40
Show Gist options
  • Save 15458434/bfa848e28054131efdf7da1f5ede590e to your computer and use it in GitHub Desktop.
Save 15458434/bfa848e28054131efdf7da1f5ede590e to your computer and use it in GitHub Desktop.
// Playground - noun: a place where people can play
import UIKit
import XCPlayground
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 667))
containerView.backgroundColor = UIColor.whiteColor()
XCPlaygroundPage.currentPage.liveView = containerView
let square = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
square.backgroundColor = UIColor.grayColor()
containerView.addSubview(square)
let animator = UIDynamicAnimator(referenceView: containerView)
let gravity = UIGravityBehavior(items: [square])
animator.addBehavior(gravity)
let barrier = UIView(frame: CGRect(x: 0, y: 300, width: 130, height: 20))
barrier.backgroundColor = UIColor.redColor()
containerView.addSubview(barrier)
let collision = UICollisionBehavior(items: [square])
collision.translatesReferenceBoundsIntoBoundary = true
animator.addBehavior(collision)
let rightEdge = CGPoint(x: barrier.frame.origin.x + barrier.frame.size.width, y: barrier.frame.origin.y)
collision.addBoundaryWithIdentifier("barrier", fromPoint: barrier.frame.origin, toPoint: rightEdge)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment