首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有附加和推送行为的UIDynamicKit会导致较长的振荡时间

带有附加和推送行为的UIDynamicKit会导致较长的振荡时间
EN

Stack Overflow用户
提问于 2014-01-30 23:03:28
回答 4查看 751关注 0票数 3

我刚刚开始使用UIDynamic工具包,并且已经在网上学习了一些示例。我已经实现了我想要的行为,这是为了在一个方向上收到一个瞬间的力,并弹回原来的位置。

我使用下面的代码实现了这一点

代码语言:javascript
复制
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个像素的末尾振荡了很长时间。更改频率和阻尼值似乎不会使情况变得更好或更糟。

还有没有人经历过这种情况?

非常感谢。

EN

回答 4

Stack Overflow用户

发布于 2014-01-31 01:16:53

添加这个,它应该会修复它:

代码语言:javascript
复制
attachment.length = 1.0f;

这是意料之中的行为,尽管它看起来很“难看”。上面的内容正好抵消了这种“不美观”。

测试过了。希望能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2014-03-11 19:25:35

我也有同样的问题。还没有解决这个问题,但我有一些想法,当UIGestureRecognizer结束时,我可以删除附件行为,但当我拖动它时,它仍然振荡:(

代码语言:javascript
复制
-(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];
}
}

这不是一个很好的解决方案,但它按我喜欢的方式工作

代码语言:javascript
复制
-(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];
}
票数 0
EN

Stack Overflow用户

发布于 2016-01-22 12:39:10

我绕过这些振荡的方法是完全删除行为,使用animator.removeBehavior(),然后将相同的行为重新添加回动画制作程序。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21459998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档