Skip to content

Instantly share code, notes, and snippets.

@ahmed-abdurrahman
Created November 7, 2015 14:58
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 ahmed-abdurrahman/1fcdf17e406de4dd3d7a to your computer and use it in GitHub Desktop.
Save ahmed-abdurrahman/1fcdf17e406de4dd3d7a to your computer and use it in GitHub Desktop.
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