Created
April 21, 2022 03:54
-
-
Save robertmryan/98e5f842f73800e88001f1b41585b50a to your computer and use it in GitHub Desktop.
Sample UIKit Dynamics w rotated view
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
class ViewController: UIViewController { | |
lazy var animator = UIDynamicAnimator(referenceView: view) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let animatedView = UIView(frame: CGRect(x: 120, y: 100, width: 50, height: 50)) | |
animatedView.transform = CGAffineTransform(rotationAngle: .pi / 4) | |
animatedView.backgroundColor = .blue | |
let fixedView = UIView(frame: CGRect(x: 110, y: 300, width: 20, height: 20)) | |
fixedView.backgroundColor = .red | |
view.addSubview(animatedView) | |
view.addSubview(fixedView) | |
let collision = UICollisionBehavior(items: [animatedView, fixedView]) | |
collision.translatesReferenceBoundsIntoBoundary = true | |
animator.addBehavior(collision) | |
let gravity = UIGravityBehavior(items: [animatedView]) | |
gravity.magnitude = 0.1 | |
animator.addBehavior(gravity) | |
let attachment = UIAttachmentBehavior(item: fixedView, attachedToAnchor: fixedView.center) | |
animator.addBehavior(attachment) | |
let configuration = UIDynamicItemBehavior(items: [fixedView]) | |
configuration.allowsRotation = false | |
animator.addBehavior(configuration) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment