Created
November 21, 2017 16:16
-
-
Save bossly/2021b5c401726090423ec5f5c9bcf9ea to your computer and use it in GitHub Desktop.
Playground UIDynamicKit - Gravity
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
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