In this answer I illustrate how to handle the dragging of a view off-screen with UIKit Dynamics. Specifically, rather than using UICollisionBehavior
(or NSTimer
or whatever), I'd suggest specifying an action
block that checks for when the views no longer intersect. This illustrates the idea when using a UIDynamicItemBehavior
, but the idea works with any UIKit dynamic behaviors:
UIDynamicItemBehavior *dynamic = [[UIDynamicItemBehavior alloc] initWithItems:@[viewToAnimate]];
[dynamic addLinearVelocity:velocity forItem:viewToAnimate];
[dynamic addAngularVelocity:angularVelocity forItem:viewToAnimate];
// when the view no longer intersects with its superview, go ahead and remove it
typeof(self) __weak weakSelf = self;
dynamic.action = ^{
if (!CGRectIntersectsRect(gesture.view.superview.bounds, gesture.view.frame)) {
[weakSelf.animator removeAllBehaviors];
[viewToAnimate removeFromSuperview];
}
};
// now add dynamic behavior
[self.animator addBehavior:dynamic];
Clearly, you should customize this to suit your particular scenario, but hopefully it illustrates the idea.