Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 823
SaldaVonSchwartz Avatar asked Nov 18 '13 02:11

SaldaVonSchwartz


2 Answers

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.

like image 127
Morkrom Avatar answered Nov 10 '22 11:11

Morkrom


you need to set the value of elasticity:

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];
like image 38
XuTao Avatar answered Nov 10 '22 11:11

XuTao