UIKit Dynamics UICollisionBehavior collision without fail

I have a view whose boundaries are set for collisions ( setTranslatesReferenceBoundsIntoBoundaryWithInsets ) and setting up a subheading with gravity so that it can collide with the boundaries of the observation.

I am trying to make a 0% collision resilient, but I have not figured out how to do this yet. I tried UIDynamicItemBehavior to spy with elasticity to 0, also with ridiculously high friction and nothing. My rationale was that 0 elasticity already means 0 power regeneration on impact, but even negative numbers seem to do nothing or very little about it.

Any ideas on how to make the collision absorb all the energy or something else to make the subview not bounce when it collides with boundaries?

+7
uikit-dynamics uicollisionbehavior uidynamicbehavior
source share
3 answers

Maybe I'm wrong, but in a brief example, it looked like this:

Highlight the UIDynamicItemBehavior for the 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 delegation 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; } 

Establishing resistance to a large value at this point seems to facilitate the element of its rebound.

+2
source share

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]; 
+1
source share

I have an idea how to do this, but have not tested it. Caution emptor!

Use UIDynamicItemBehavior in your view. Set the resistance to a high value after a collision using the UICollisionBehaviorDelegate collisionBehavior: finishedContactForItem: withItem method. Resistance is like friction between an object and a vision. 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]; } 
0
source share

All Articles