SetAnimationRepeatAutoreverses does not behave as I expected

I'm starting to learn how to use UIView animation. So I wrote the following lines:

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:2.0];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationRepeatAutoreverses:YES];

CGPoint position = greenView.center;
position.y = position.y + 100.0f;
position.x = position.x + 100.0f;
greenView.center = position;

[UIView commitAnimations];

In this case, the UIView (green box) moves back and forth 2 times. So far so good, BUT I found out that after a double transition the green box turned up to jump to the “new position” (position.x + 100.0f, position.y + 100.0f) instead of returning to its original position (position.x, position.y ) This makes the animation rather strange (for example, after the window returns to its original position caused by setAnimationRepeatAutoreverses, it returns to its new position within the last microsecond!)

What is the best way to get the green box NOT to move to the new position at the last minute?

+5
2

UIView alpha. , "" animationDidStop: delegate, , .

, animationDidStop :

- (void)performAnimation
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(phase1AnimationDidStop:finished:context:)];

    // Do phase 1 of your animations here
    CGPoint center = someView.center;
    center.x += 100.0;
    center.y += 100.0;
    someView.center = center;

    [UIView commitAnimations];
}

- (void)phase1AnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(phase2AnimationDidStop:finished:context:)];

    // Do phase 2 of your animations here
    CGPoint center = someView.center;
    center.x -= 100.0;
    center.y -= 100.0;
    someView.center = center;

    [UIView commitAnimations];
}

- (void)phase2AnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    // Perform cleanup (if necessary)
}

, . , Apple [UIView setAnimationFinishesInOriginalState:] - , , .

+1

, setAnimationDuration , . setAnimationDuration , . , setAnimationDidStopSelector:.

// Repeat Swimming
CGPoint position = swimmingFish.center;
position.y = position.y - 100.0f;
position.x = position.x - 100.0f;

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.0f];
[UIView setAnimationRepeatCount:2.5];
[UIView setAnimationRepeatAutoreverses:YES];

swimmingFish.center = position;
+2

All Articles