How to implement UIViewAnimationOptionBeginFromCurrentState

2.2k Views Asked by At

I have a piece of audio & associated Play & Stop buttons, when the Play button is pressed I use an animation of a curser to denote at which point in the audio sample we are, at a given moment. Whenever the stop button is pressed I want my curser to return to its initial coordinates. I believe UIViewAnimationOptionBeginFromCurrentState might be the way to do this? My code for the initial animation is below, anyone have any tips on how to use UIViewAnimationOptionBeginFromCurrentState in order to send the cursor back to its original coordinates?

Thanks for any help in advance :)

    [UIView beginAnimations:@"MoveView" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:2.0f];
    yellowBar.frame = CGRectMake(310, 20, 5, 100);
    [UIView commitAnimations];
3

There are 3 best solutions below

0
On BEST ANSWER

If you are still using beginAnimations/commitAnimations, use setAnimationBeginsFromCurrentState:. Otherwise, you can pass UIViewAnimationOptionBeginFromCurrentState to animateWithDuration:delay:options:animations:completion: for the options parameter.

0
On

AFAIK you can only use that option with block-based animations (which you are encouraged to use anyway). You'd use the animateWithDuration:delay:options:animations:completion: method, described here.

0
On

Brian is right, you have got two alternatives:

//Initialize newFrame here

[UIView animateWithDuration:0.5
                      delay:0.0
                    options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState //Multiple options
                 animations:^ {

                     [yourView setFrame: newFrame];
                 }
                 completion:^ (BOOL finished) {

                 }];

Or

//Initialize newFrame here

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];

    [yourView setFrame: newFrame];

[UIView commitAnimations];