通过添加具有阻力的UIDynamicItemBehavior,我可以轻松地使捕捉的慢一些。但是,电阻的默认值为0.0,这对于我来说仍然太慢。将电阻设置为负值没有影响,它的移动速度似乎可以达到0.0。
如何使UISnapBehavior更快?
(以下是使捕捉的变慢的示例):
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];
最佳答案
您还可以使用UIAttachmentBehavior
来实现与UISnapBehavior
类似的效果,并更好地控制速度。例如:
UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:viewToAnimate attachedToAnchor:viewToAnimate.center];
[self.animator addBehavior:attachment];
attachment.frequency = 20.0;
attachment.damping = 1.0;
attachment.anchorPoint = newPoint;
通过将
frequency
增加到1.0
以上的值将使其速度更快。通过将frequency
减小为0.0
和1.0
之间的值,将会使其速度变慢(或通过将resistance
值大于1.0
的值添加到UIDynamicItemBehavior
中)。如果您在使用此
frequency
值时发现它在该最终位置振荡,则也为该项添加一些阻力:UIDynamicItemBehavior *resistance = [[UIDynamicItemBehavior alloc] initWithItems:@[viewToAnimate]];
resistance.resistance = 100.0;
[self.animator addBehavior:resistance];