我怎样才能加快UISnapBehavior的速度?

时间:2014-02-07 10:05:52

标签: ios objective-c cocoa-touch uikit-dynamics

我可以通过添加带阻力的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];

1 个答案:

答案 0 :(得分:5)

您还可以使用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.01.0之间的值会使其变慢(或将resistance值大于1.0的值添加到UIDynamicItemBehavior )。


如果您在使用此frequency值时发现它在最终位置振荡,请为该项添加一些阻力:

UIDynamicItemBehavior *resistance = [[UIDynamicItemBehavior alloc] initWithItems:@[viewToAnimate]];
resistance.resistance = 100.0;
[self.animator addBehavior:resistance];