Question

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];
Was it helpful?

Solution

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top