Skip to content

Instantly share code, notes, and snippets.

@bossly
Created November 21, 2017 16:16
Show Gist options
  • Save bossly/2021b5c401726090423ec5f5c9bcf9ea to your computer and use it in GitHub Desktop.
Save bossly/2021b5c401726090423ec5f5c9bcf9ea to your computer and use it in GitHub Desktop.
Playground UIDynamicKit - Gravity
import UIKit
import PlaygroundSupport
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 667))
containerView.backgroundColor = UIColor.white
PlaygroundPage.current.liveView = containerView
let square = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
square.backgroundColor = UIColor.gray
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.red
containerView.addSubview(barrier)
let collision = UICollisionBehavior(items: [square])
collision.translatesReferenceBoundsIntoBoundary = true
animator.addBehavior(collision)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment