开发者

iPhone - UIView Animation not looping correctly

开发者 https://www.devze.com 2022-12-28 23:19 出处:网络
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 the

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 animatio开发者_运维问答n. 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消