2014-01-30 21 views
3

我剛開始使用UIDynamic套件,並且已在線跟蹤示例。我已經達到了我想要的行爲,這是爲了在一個方向上獲得瞬間的力量並且回到原來的位置。具有附件和推送行爲的UIDynamicKit會導致較長的振盪時間

我實現這一點使用下面

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個像素的時間很長。改變頻率和阻尼值似乎並沒有使這個更好或更糟。

有沒有其他人經歷過這個?

很多謝謝。

回答

2

添加這一點,它應該修復它:

attachment.length = 1.0f; 

這是一個預期的行爲,雖然看上去「不堪入目」。以上只是抵消了「難看」。

經過測試。希望能幫助到你。

+0

我有這個問題,1.0的長度似乎首先延遲振盪,但不是永遠。最終在足夠的動作之後,觀點仍然會振盪。 – snakeoil

0

我有同樣的問題。不解決這個問題,但是我有一些想法 我刪除依戀行爲時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]; 
} 
0

我圍繞這些振盪得到的辦法是徹底消除了行爲,使用animator.removeBehavior(),然後重新添加相同的行爲回到動畫

0

添加這種行爲:

dynamic = UIDynamicItemBehavior() 
    dynamic.resistance = 1 
    self.animator.addBehavior(dynamic)