3

I am just starting to use the UIDynamic kit and have followed examples online. I have achieved the behaviour I want, which is for a view to recieved an instantaneous force in a direction and spring back to the original spot.

I achieved this using the code below

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];

However, it oscillates for a very long time at the end over 1 or 2 pixels. Changing the frequency and damping values doesn't seem to make this any better or worse.

Has anyone else experienced this?

Many Thanks.

noRema
  • 559
  • 5
  • 13

4 Answers4

2

Add this and it should fix it:

attachment.length = 1.0f;

It is an expected behavior, although it seems "unsightly". The above just offsets that "unsightliness".

Tested. Hope it helps.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • I have this problem, and length of 1.0 does seem to delay the oscillations at first, but not forever. Eventually after enough movements, the views will oscillate still. – snakeoil Jan 22 '16 at 03:02
0

i have the same problem. didn't solve it yet, but i have some ideas i remove attachment behavior when UIGestureRecognizer ended, but when i drag it still oscillates :(

-(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];
}
}

and this is not pretty solution but it works as i like

-(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];
}
Eugene_skr
  • 79
  • 3
0

The way I got around these oscillations was by removing the behavior entirely, using animator.removeBehavior() and then re-adding the same behavior back to the animator.

snakeoil
  • 497
  • 4
  • 12
0

Add this behaviour:

    dynamic = UIDynamicItemBehavior()
    dynamic.resistance = 1
    self.animator.addBehavior(dynamic)
J. Doe
  • 12,159
  • 9
  • 60
  • 114