9

我有一个视图,其边界设置为碰撞(setTranslatesReferenceBoundsIntoBoundaryWithInsets)和一个带有重力的子视图设置,以便它可以与超级视图边界发生碰撞。

我正在尝试使碰撞的弹性为 0%,但我还没有弄清楚如何。我尝试了UIDynamicItemBehavior弹性为 0 的子视图,摩擦力也高得离谱,什么也没有。我的理由是 0 弹性已经意味着 0 力在冲击时再生,但即使是负数似乎也无济于事或对此无能为力。

关于如何使碰撞吸收所有能量或使子视图在与边界碰撞时不反弹的任何想法?

4

3 回答 3

3

我可能做错了,但以下似乎在一个简短的例子中起作用:

为有问题的项目分配 UIDynamicItemBehavior:

self.itemBehaviorInQuestion = [[UIDynamicItemBehavior alloc] initWithItems:@[self.infoView]];
self.itemBehaviorInQuestion.resistance = 0;

self.collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.infoView]];            

self.collisionBehavior.collisionDelegate = self;

[self.animator addBehavior:self.collisionBehavior];
[self.animator addBehavior:self.itemBehaviorInQuestion];

实现以下 UICollisionBehavior 委托方法:

- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p
{
    self.itemBehaviorInQuestion.resistance = 100;
}

- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier
{
    self.itemBehaviorInQuestion.resistance = 0;
}

在那一刻将阻力设置为高值似乎可以减轻项目的反弹。

于 2014-05-13T19:28:59.967 回答
1

您需要设置弹性值:

UIView* square = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.view addSubview:square

UIDynamicAnimator* a =  [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIDynamicItemBehavior* behavior = [[UIDynamicItemBehavior alloc] initWithItems:@[square]];
behavior.elasticity = 0.5;
[a addBehavior:behavior];
于 2014-03-14T03:54:48.010 回答
0

我有一个想法如何做到这一点,但还没有测试过。买者自负!

在您的视图上使用 UIDynamicItemBehavior。使用 UICollisionBehaviorDelegate 方法 collisionBehavior:endedContactForItem:withItem 将碰撞后的阻力设置为较高的值。“阻力”就像物品和拥有者视图之间的摩擦。这是草图...

UIDynamicItemBehavior *lowFriction = [[UIDynamicItemBehavior alloc] init];
lowFriction.resistance = 0.0; 

UIDynamicItemBehavior *highFriction = [[UIDynamicItemBehavior alloc] init];
highFriction.resistance = 10.0; 

yourCollidingView *ycv = [[yourCollidingView alloc] init];  // yourCollidingView is a subclass of UIView

[noFriction addItem:ycv];

[myAnimator addBehavior:lowFriction];
[myAnimator addBehavior:highFriction];

然后在委托

- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id<UIDynamicItem>)item1 withItem:(id<UIDynamicItem>)item2 {

    [lowFriction removeItem:item1];
    [lowFriction removeItem:item2];

    [highFriction addItem:item1];
    [highFriction addItem:item2];

}
于 2013-11-18T05:51:31.370 回答