UIDynamicAnimator视图从引用边界外部进入 [英] UIDynamicAnimator view entering from outside of the reference bounds

查看:114
本文介绍了UIDynamicAnimator视图从引用边界外部进入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 UIDynamicAnimator 和重力/碰撞/弹性效果来放置对象。我查看了Apple的示例应用程序 DynamicsCatalog ,它是非常直接,除了当对象从容器的边界开始时。

I would like to animate dropping an object using the UIDynamicAnimator and gravity/collision/elasticity effects. I looked at Apple's sample app DynamicsCatalog, and it is quite straight forward, except for when the object starts off from the outside the bounds of its container.

例如,这是从示例应用程序的<$ c中获取的代码$ c> APLCollisionGravityViewController.m file:

For example, this is the code taken from the sample app's APLCollisionGravityViewController.m file:

UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[self.square]];
[animator addBehavior:gravityBeahvior];

UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.square]];
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
collisionBehavior.collisionDelegate = self;
[animator addBehavior:collisionBehavior];

如果 self.square的框架最初完全在 self.view 之内。如果我更改它以使其具有负 frame.origin.y 值,则事情变得更加奇怪。具体来说,如果 frame.origin.y 的绝对值超过 frame.size.height 的一半, square似乎是吸引而不是下降 - 它上升而不是下降。

It works well if the frame of self.square is initially completely within self.view. If I change it so that it has a negative frame.origin.y value, things get stranger. Specifically, if the absolute value of frame.origin.y is more than half of frame.size.height, the square appears to be "gravitating" up instead of down - it rises instead of falling.

我正在寻找最初完全在容器范围之外的方块(即,我设置 frame.origin.y = -frame.size.height ),如何修改动画师/重力/碰撞行为以适应这种情况?

I am looking for have the square initially completely outside of the container bounds (that is, I set frame.origin.y = -frame.size.height), how can I modify the animator/gravity/collision behaviours to accommodate that?

推荐答案

问题是,当对象试图进入时,你正撞到顶部边界并导致碰撞参考视图!对象从外部掉落并从参考视图的顶部反弹。

The problem is that you're banging into the top boundary and causing a collision when the object tries to enter the reference view! The object is falling from outside and bouncing off the top of your reference view.

因此,您对碰撞边界的定义过于简单:

Thus your definition of the collision boundary is too simple-minded:

collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;

不。您必须手动设置碰撞边界。例如,从一个只是参考视图的 bottom 的边界开始,这样对象就可以进入场景并从地板反弹,如下所示:

No. You'll have to set up the collision boundaries manually. For example, start with a boundary that's just the bottom of the reference view, so that the object can enter the scene and bounce off the floor, like this:

UICollisionBehavior* coll = [UICollisionBehavior new];
coll.collisionMode = UICollisionBehaviorModeBoundaries;
coll.collisionDelegate = self;
[coll addBoundaryWithIdentifier:@"floor"
                      fromPoint:CGPointMake(0,self.view.bounds.size.height)
                        toPoint:CGPointMake(self.view.bounds.size.width,
                                            self.view.bounds.size.height)];

如果要在之后更改为另一组封闭的边界第一次碰撞,很好,但首先你必须让对象进入参考视图。现在你用碰撞边界锁定了它。

If you want to change to a different, enclosing set of boundaries after the first collision, fine, but first you have to let the object into the reference view. Right now you're locking it out with your collision boundaries.

这篇关于UIDynamicAnimator视图从引用边界外部进入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆