Skip to content

Instantly share code, notes, and snippets.

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 mayoralito/2162a1b94bf6d311912c6f53a71911f9 to your computer and use it in GitHub Desktop.
Save mayoralito/2162a1b94bf6d311912c6f53a71911f9 to your computer and use it in GitHub Desktop.
radars
class ViewController: UIViewController {
var animator: UIDynamicAnimator!
var gravity: UIGravityBehavior!
var collision: UICollisionBehavior!
override func viewDidLoad() {
super.viewDidLoad()
// --------------------------------------
//
let customView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
// more code....
//
animator = UIDynamicAnimator(referenceView: view)
// mode code....
//
collision = UICollisionBehavior(items: [customView])
collision.collisionDelegate = self
// more code
animator.addBehavior(collision)
}
}
extension ViewController: UICollisionBehaviorDelegate {
// This will be marked as a warning suggesting to add private property because it nearly matches one of the UICollisionBehaviorDelegate methods.
// but it's the same deinition.
func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?, at p: CGPoint) {
print("Boundary contact occurred - \(String(describing: identifier))")
guard let collidingView = item as? UIView else { return }
collidingView.backgroundColor = UIColor.yellow
UIView.animate(withDuration: 0.3) {
collidingView.backgroundColor = UIColor.gray
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment