3

I can easily make the snapping slower by adding a UIDynamicItemBehavior with resistance. However, the default value for resistance is 0.0, which is still too slow for me. Setting the resistance to a negative value has no effect, it seems move as fast as 0.0.

How can I make the UISnapBehavior faster?

(Here is an example of making the snapping slower):

UIDynamicItemBehavior *dynamicItemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[button]];
dynamicItemBehavior.resistance = 50.0; // This makes the snapping SLOWER
UISnapBehavior *snapBehavior = [[UISnapBehavior alloc] initWithItem:button snapToPoint:point];
[self.animator addBehavior:button.dynamicItemBehavior];
[self.animator addBehavior:button.snapBehavior];
Hunkpapa
  • 724
  • 5
  • 17
  • where does the "snapped" item start ? By increasing the distance between start and destination, you'll increase the speed – KIDdAe Feb 07 '14 at 10:10
  • Thanks @KIDdAe , that's a good idea, it might work for my use case this time, but it would still be nice to be able to speed up the snapping speed itself, maybe by specifying duration somewhere? – Hunkpapa Feb 07 '14 at 10:21

1 Answers1

5

You can also use a UIAttachmentBehavior to achieve a similar affect as UISnapBehavior, with greater control over speed. For example:

UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:viewToAnimate attachedToAnchor:viewToAnimate.center];
[self.animator addBehavior:attachment];
attachment.frequency = 20.0;
attachment.damping = 1.0;
attachment.anchorPoint = newPoint;

By increasing frequency to values above 1.0 will make it faster. By decreasing frequency to values between 0.0 and 1.0, will make it slower (or by adding resistance values greater than 1.0 to your UIDynamicItemBehavior).


If you find it oscillating at that final location when using this frequency value, add some resistance to the item, too:

UIDynamicItemBehavior *resistance = [[UIDynamicItemBehavior alloc] initWithItems:@[viewToAnimate]];
resistance.resistance = 100.0;
[self.animator addBehavior:resistance];
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    Thanks for this. Is there any way to stop the item from oscillating once it has reached the anchor point? The snap will animate to the point and then stop. This seems to animate to the point but never stop oscillating. – Fogmeister Sep 17 '14 at 15:25
  • @Fogmeister You can add some large resistance to the item and that will eliminate the oscillation. – Rob Sep 17 '14 at 18:23