What is the best way to use UICollisionBehavior to detect when a view is no longer displayed on the screen?

I am trying to use UICollisionBehaviorin UIKit Dynamics to find out when the view that I selected on the screen (using UIAttachmentBehaviorand UIPushBehavior) is actually completely turned off.

I find it difficult because I cannot track it as it progresses once it has been cast. I am trying to figure out using UICollisionBehaviorto detect when his last edge "collided" with his superview. This seems to be the easiest way to find out when it is turned off compared to the NSTimer solution or something similar (but if you can think of any easier way, I'm all ears!).

The solution I saw in this project (in particular here ):

CGRect referenceBounds = self.animator.referenceView.bounds;
CGFloat inset = -hypot(CGRectGetWidth(referenceBounds), CGRectGetHeight(referenceBounds));
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(inset, inset, inset, inset);
[self.collisionBehavior setTranslatesReferenceBoundsIntoBoundaryWithInsets:edgeInsets];

Which calculates where the boundaries of the collisions I guess (to be honest, I do not quite understand this), and then when the delegate is called when a collision is detected, I call removeFromSuperview.

But for some reason this works very unjustifiably. Sometimes I throw him out of the screen and he will never call the delegate detected by the collision in any way. Often time will be a little later.

As for my setup, it just drops the UIScrollView screen, which has its own frame set to the borders of its supervisor ( self.viewin the view controller).

Is there a better way to set up collision detection when it leaves a view?

+4
1

, UIKit Dynamics. , UICollisionBehavior ( NSTimer - ) action, , . UIDynamicItemBehavior, UIKit:

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];

, , , , .

+4

All Articles