I'm trying to accelerate a UIView over a time/distance. So far it seems that UIPushBehavior
is continuous, i.e it doesn't stop until it hits an immovable object. Is there a way to accelerate a UIView
over time? or a distance?
P.S I'm not great in terms of physics vernacular, so please be kind haha
A couple of thoughts:
If you want to slow down the push over time, so that a single push translates to some finite distance traveled, you have to (a) add a linear resistance
(so it slows down); and (b) use UIPushBehaviorModeInstantaneous
(so it doesn't keep pushing). To add resistance
, you would add a UIDynamicItemBehavior
:
UIDynamicItemBehavior *resistanceBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[viewToAnimate]];
resistanceBehavior.resistance = 1.0;
[self.animator addBehavior:resistanceBehavior];
In terms of controlling the distance that each push generates, that's a combination of (a) the magnitude of the pushDirection
vector you employ; (b) the resistance
you apply to your item in the UIDynamicItemBehavior
; (c) the size of item you're moving; and (d) the density
you apply to your item in UIDynamicItemBehavior
. It's probably best to play around with those variables until you achieve the desired distance for a single push.
If, on the other hand, you want to "accelerate a UIView
over time", then rather than a UIPushBehaviorModeInstantaneous
, you would apply a UIPushBehaviorModeContinuous
. By continuously applying a force, it will continue to accelerate in the pushDirection
until that UIPushBehavior
is removed from the UIDynamicAnimator
.