简体   繁体   中英

iPhone - UIView Animation not looping correctly

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!

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!

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: setAnimationRepeatAutoreverses not behaved as I expected

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM