UIKit Dynamics UICollisionBehavior collision without bounce

I have a view whose boundaries are set up for collisions ( setTranslatesReferenceBoundsIntoBoundaryWithInsets ) and a subview setup with gravity so that it can collide against the superview bounds.

I'm trying to make the collision 0% bouncy, but I haven't figured out yet how. I tried a UIDynamicItemBehavior for the subview with elasticity to 0, also with ridiculously high friction and nothing. My rationale was that 0 elasticity already means 0 force regeneration on impact but even negative numbers seem to do nothing or very little about it.

Any ideas as to how to make the collision absorb all energy or whatever it takes to make the subview not bounce when it collides against the bounds?


I may be doing it wrong, but the following seemed to work in a brief example:

Allocate UIDynamicItemBehavior for item(s) in question:

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

Implement the following UICollisionBehavior delegate methods:

- (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;
}

Setting resistance to a high value at that moment seems to relieve the item of its bounce.


您需要设置弹性的值:

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

I have an idea how to do it, but haven't tested it. Caveat emptor!

Use UIDynamicItemBehavior on your view. Set resistance to a high value after the collision, using the UICollisionBehaviorDelegate method collisionBehavior:endedContactForItem:withItem. 'Resistance' is like friction between the item and the owning view. Here's a sketch...

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

Then in the delegate

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

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

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

}
链接地址: http://www.djcxy.com/p/17404.html

上一篇: 我应该使用JSLint还是JSHint JavaScript验证?

下一篇: UIKit Dynamics UICollisionBehavior碰撞没有反弹