Created
January 20, 2017 19:05
-
-
Save oaleeapp/53d3f5fbffb1951d9549fcb4b66dbc65 to your computer and use it in GitHub Desktop.
A example of UIDynamic Classes
This file contains 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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment