Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detecting collisions between views.
// 1. Conform to UICillisionBehaviorDelegate
AnimationViewController: UIViewController, UICollisionBehaviorDelegate
// 2. Handling collision
// This function will be called with every and each collision starts between all the views added to our collision behavior.
func collisionBehavior(behavior: UICollisionBehavior, beganContactForItem item1: UIDynamicItem, withItem item2: UIDynamicItem, atPoint p: CGPoint){
// Wer're only interested in collisions with the black circle (snappingCircle).
if (item1 as? UIView)?.tag == snappingCirlceTag || (item2 as? UIView)?.tag == snappingCirlceTag {
displayCollisionMessage(message)
}
}
// 3. Setting self as the collision bahavior delegate
lazy var squareBehavior: SquareBehavior = {
let lazySquareBehavior = SquareBehavior(settings: self.animationSettings)
self.animator.addBehavior(lazySquareBehavior)
// Here..
lazySquareBehavior.collider.collisionDelegate = self
return lazySquareBehavior
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment