views:

188

answers:

3

Hey all, got a little problem here and can't figure out what I am doing wrong. I am trying to animate a UIView up and down repeatedly. When I start it, it goes down correctly and then back up but then immediately shoots to the "final" position of the animation. Code is as follows:

UIImageView *guide = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
    guide.frame = CGRectMake(250, 80, 30, 30);

    [self.view addSubview:guide];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:2];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationBeginsFromCurrentState:YES];

    guide.frame = CGRectMake(250, 300, 30, 30);

    [UIView setAnimationRepeatCount:10];
    [UIView commitAnimations];

Thanks in advance!

A: 

It's a 'feature', not a bug. It's really annoying, but it looks like unless Apple says otherwise, this is how UIView Animations are expected to work.

See this answer: http://stackoverflow.com/questions/2516184/setanimationrepeatautoreverses-not-behaved-as-i-expected/2570064#2570064

Nick Forge
A: 

You may set setAnimationDidStopSelector to a custom function and in the custom function you can set up more animations (even recursively). See my answer to another question here.

Usually I will define an animation structure (AnimationPath in the example) to coordinate the whole animation (say, by a counter).

ohho
A: 

This is an old question, but in case anyone is looking at this you can just use a fractional repeat count to end on the desired state. If you're animating from A to B and using auto reverse, each repeat will look like this A -> B -> A. Since you want to end on B on the last animation and not A, just do this to make the last repeat only go halfway (to B):

[UIView setAnimationRepeatCount:10.5];

That's it!

atticus