0

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

Adam Carter
  • 4,741
  • 5
  • 42
  • 103

1 Answers1

2

A couple of thoughts:

  1. 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.

  2. 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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Hey @rob -- can you attach a "spring" in Dynamics? So, we start at 0,0. You add a UIPushBehaviorModeInstantaneous, let's say to the left. the spring concept will make it, well, behave like a spring, it will bounce back and fore ultimately settling again at 0,0. This is possible with UIKit Dynamics? Cheers! – Fattie Jun 23 '14 at 07:22
  • @JoeBlow You can achieve spring effects in a couple of ways, but I'm not sure it makes sense within the context of a push behavior. It makes more sense in the context of a `UIAttachmentBehavior` (generally linked to some continuous gesture) or a `UISnapBehavior` (generally linked to a discrete gesture). If you're still unclear after researching/experimenting with those behaviors, I'd suggest you post a new question, rather than discussing further in the comments here. – Rob Jun 23 '14 at 12:47
  • Hi @Rob, thanks for the reply. I've got the perfect example, making a "wrong-shake" the same physics as UIKit Dynamics ... http://stackoverflow.com/questions/24356051 (unrelated to gestures; the app initiates it in this case) Cheers! – Fattie Jun 23 '14 at 12:50
  • Ah, I see what you meant. Yes, you can achieve that with attachment behavior and push behavior, tho I'd use block-based animation, personally. See my answer to that other question. – Rob Jun 23 '14 at 14:26