我刚刚开始使用UIDynamic工具包,并且已经在网上学习了一些示例。我已经实现了我想要的行为,这是为了在一个方向上收到一个瞬间的力,并弹回原来的位置。
我使用下面的代码实现了这一点
CGPoint origCenter = self.viewContentContainer.center;
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIPushBehavior *push = [[UIPushBehavior alloc]
initWithItems:@[self.viewContentContainer] mode:UIPushBehaviorModeInstantaneous];
CGVector vector = CGVectorMake(200.0, 0.0);
push.pushDirection = vector;
CGPoint anchorpoint = origCenter;
UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:self.viewContentContainer attachedToAnchor:anchorpoint];
[attachment setFrequency:2.0];
[attachment setDamping:0.5];
[self.animator addBehavior:push];
[self.animator addBehavior:attachment];然而,它在超过1或2个像素的末尾振荡了很长时间。更改频率和阻尼值似乎不会使情况变得更好或更糟。
还有没有人经历过这种情况?
非常感谢。
发布于 2014-01-31 01:16:53
添加这个,它应该会修复它:
attachment.length = 1.0f;这是意料之中的行为,尽管它看起来很“难看”。上面的内容正好抵消了这种“不美观”。
测试过了。希望能有所帮助。
发布于 2014-03-11 19:25:35
我也有同样的问题。还没有解决这个问题,但我有一些想法,当UIGestureRecognizer结束时,我可以删除附件行为,但当我拖动它时,它仍然振荡:(
-(void) handlePanGestureRecognizer:(UIPanGestureRecognizer*) gr
{
CGPoint att = [gr locationInView:self.view];
self.attachView.center = att;
if (gr.state == UIGestureRecognizerStateBegan)
{
_attachment.anchorPoint = att;
[_animator addBehavior:_attachment];
if (_snap)
[_animator removeBehavior:_snap];
}
if (gr.state == UIGestureRecognizerStateChanged)
{
_attachment.anchorPoint = att;
}
if (gr.state == UIGestureRecognizerStateEnded)
{
_snap = [[UISnapBehavior alloc] initWithItem:self.button snapToPoint:att];
[_animator addBehavior:_snap];
[_animator removeBehavior:_attachment];
}
}这不是一个很好的解决方案,但它按我喜欢的方式工作
-(void) handlePanGestureRecognizer:(UIPanGestureRecognizer*) gr
{
CGPoint att = [gr locationInView:self.view];
self.attachView.center = att;
if (_snap)
[_animator removeBehavior:_snap];
_snap = [[UISnapBehavior alloc] initWithItem:self.button snapToPoint:att];
[_animator addBehavior:_snap];
}发布于 2016-01-22 12:39:10
我绕过这些振荡的方法是完全删除行为,使用animator.removeBehavior(),然后将相同的行为重新添加回动画制作程序。
https://stackoverflow.com/questions/21459998
复制相似问题