-
-
Save ahmed-abdurrahman/1fcdf17e406de4dd3d7a to your computer and use it in GitHub Desktop.
Detecting collisions between views.
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
// 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