title: How would I best use UICollisionBehavior to detect when a view is no longer on screen at all?
body: <p>I'm trying to use <code>UICollisionBehavior</code> in UIKit Dynamics to figure out when a view that I've thrown off screen (using <code>UIAttachmentBehavior</code> and <code>UIPushBehavior</code>) is actually fully off screen.</p>
  
  <p>I'm finding it complicated because I'm not able to track it as it progresses, once it's thrown I'm trying to figure out using <code>UICollisionBehavior</code> to detect when its last edge has "collided" with its superview. This seems to be the easiest way to figure out when it's off screen compared to an NSTimer solution or something similar (but if you can think of any easier way, I'm all ears!).</p>
  
  <p>A solution I saw in <a href="https://github.com/toco/Dynamic-ImageView" rel="nofollow">this project</a> (specifically <a href="https://github.com/toco/Dynamic-ImageView/blob/master/Dynamic%20ImageView/TOCDynamicImageViewController.m#L78" rel="nofollow">here</a>) was as follows:</p>
  
  <pre><code>CGRect referenceBounds = self.animator.referenceView.bounds;
  CGFloat inset = -hypot(CGRectGetWidth(referenceBounds), CGRectGetHeight(referenceBounds));
  UIEdgeInsets edgeInsets = UIEdgeInsetsMake(inset, inset, inset, inset);
  [self.collisionBehavior setTranslatesReferenceBoundsIntoBoundaryWithInsets:edgeInsets];
  </code></pre>
  
  <p>Which calculates where the collision bounds are I'm guessing (to be honest I don't completely understand it) and then when the delegate gets called when the collision is detected I call <code>removeFromSuperview</code>.</p>
  
  <p>But for whatever reason this works very unreliably. Sometimes I'll throw it off screen and it'll actually never call the collision detected delegate somehow. Often the timing will be a little late as well.</p>
  
  <p>As far as my setup goes it's just throwing a UIScrollView off screen that has its frame set to the bounds of its superview (<code>self.view</code> in the view controller).</p>
  
  <p>Is there a better way to setup collision detection for when it leaves the view?</p>