Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Created April 21, 2022 03:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertmryan/98e5f842f73800e88001f1b41585b50a to your computer and use it in GitHub Desktop.
Save robertmryan/98e5f842f73800e88001f1b41585b50a to your computer and use it in GitHub Desktop.
Sample UIKit Dynamics w rotated view
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