I would like to know if it is possible to call a function when a UIView comes into contact with a collision boundary, but not to have them actually collide.
Can a function be called when they come into contact and have the view go right through the boundary?
If so, how?
Yes, UIKit
framework provides a nice collision detection facility.
To respond to UIKit dynamic item collisions (e.g., views) configure a custom class to adopt the UICollisionBehaviorDelegate
protocol. Then, in a collision behavior (an instance of the UICollisionBehavior
class), set the delegate to be an instance of your custom class.
In the aforementioned UICollisionBehavior
add your view to it and configure your desired boundary using one of the addBoundary
methods. Your delegate will then be notified of a collision in the collisionBehavior(_:beganContactFor:withBoundaryIdentifier:at:)
protocol function.
(The default collisionMode
is everything
, so you can safely ignore this property for now.)