Search code examples
iphoneobjective-cuiviewcore-animation

Troubles with UIView, animateWithDuration and beginFromCurrentState


I have a custom view that has to track the user's location. I put the following code in touchesBegan, as well as in touchesMoved:

[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
    cursorView.center = locationOfTouch;
} completion:^(BOOL finished){}];

It seems fairly straightforward to me. I'd expect the view to always animate to the user's current location, even if that location is changed and the view is still animating (because of the beginFromCurrentState option).

Yet, each animation finishes completely. They don't 'transition' to the new animation, no they finish first, then they start the new animation.

I tried adding this line in touchesMoved:

[cursorView.layer removeAllAnimations];

Doesn't do anything. No animation is cancelled. Any ideas?


Solution

  • with [cursorView.layer removeAllAnimations]; you access the layer of the view but you have set the animation not to the layer but the view directly.
    Try:

    [UIView beginAnimations:nil context:NULL]
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    cursorView.center = locationOfTouch;
    [UIView commitAnimations];
    

    and leave out the removeAllAnimations and it should transition from current state.