Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I speed up UISnapBehavior?

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];
like image 239
Hunkpapa Avatar asked Dec 21 '25 12:12

Hunkpapa


1 Answers

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];
like image 76
Rob Avatar answered Dec 24 '25 05:12

Rob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!