简体   繁体   English

我如何最好地使用UICollisionBehavior来检测视图何时不在屏幕上?

[英]How would I best use UICollisionBehavior to detect when a view is no longer on screen at all?

I'm trying to use UICollisionBehavior in UIKit Dynamics to figure out when a view that I've thrown off screen (using UIAttachmentBehavior and UIPushBehavior ) is actually fully off screen. 我正试图在UIKit Dynamics中使用UICollisionBehavior来弄清楚我从屏幕上抛出的视图(使用UIAttachmentBehaviorUIPushBehavior )实际上完全不在屏幕上。

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 UICollisionBehavior to detect when its last edge has "collided" with its superview. 我发现它很复杂,因为我无法跟踪它的进展,一旦它被抛出我试图找出使用UICollisionBehavior来检测它的最后一个边缘何时与其UICollisionBehavior视图“相撞”。 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!). 与NSTimer解决方案或类似的解决方案相比,这似乎是最简单的方法来判断它是否在屏幕外(但如果您能想到任何更简单的方法,我都会耳朵!)。

A solution I saw in this project (specifically here ) was as follows: 我在这个项目中看到的解决方案(具体在这里 )如下:

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 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 removeFromSuperview . 其中计算碰撞边界的位置我猜测(说实话我并不完全理解它)然后当检测到碰撞时调用委托时我调用removeFromSuperview

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. 通常时间也会有点晚。

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 ( self.view in the view controller). 就我的设置而言,它只是抛出一个UIScrollView关闭屏幕,其框架设置为其超级视图的边界(视图控制器中的self.view )。

Is there a better way to setup collision detection for when it leaves the view? 有没有更好的方法来设置碰撞检测何时离开视图?

In this answer I illustrate how to handle the dragging of a view off-screen with UIKit Dynamics. 这个答案中,我将说明如何使用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. 具体来说,我建议不要使用UICollisionBehavior (或NSTimer或其他),而是指定一个action块来检查视图何时不再相交。 This illustrates the idea when using a UIDynamicItemBehavior , but the idea works with any UIKit dynamic behaviors: 这说明了使用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];

Clearly, you should customize this to suit your particular scenario, but hopefully it illustrates the idea. 显然,您应该根据您的特定情况对其进行自定义,但希望它能够说明这一想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在视图屏幕周围拖动UITextView? - How would I drag UITextView around the view screen? 如何绘制一个与UICollisionBehavior相似的圆圈UIView? - How do I draw a circle UIView that acts like one with UICollisionBehavior? 为什么我无法检测到何时不再可以看到使用UIKit Dynamics推送的UIView? - Why am I unable to detect when my UIView I push off screen using UIKit Dynamics is no longer visible? 如何检测moreNavigationController中是否不再存在viewController? - How can I detect if a viewController is no longer in the moreNavigationController? 如何检测屏幕上没有手指? - How to detect when no fingers are on the screen? 如何使用autolayout缩放所有屏幕尺寸的视图? - How to use autolayout to scale view for all screen sizes? 如何旋转时如何让它淡入另一个视图? - How would I get it to fade to another view when rotated? 如何创建对角线切割视图以在配置文件屏幕中使用? - How can I create diagonal cut view to use in profile screen? 当我再次遇到特定对象时,如何最好地识别它们? - How would I best go about identifying specific objects when I come across them again? 加载视图时,如何使图像覆盖屏幕? - How do I make an image cover the screen when a view is loading?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM