iPhone - UIView 动画无法正确循环

发布于 2024-08-30 17:54:04 字数 676 浏览 3 评论 0原文

大家好,这里遇到一个小问题,不知道我做错了什么。我正在尝试反复上下动画 UIView 。当我启动它时,它会正确下降然后返回,但然后立即拍摄到动画的“最终”位置。代码如下:

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];

先谢谢了!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲性难收 2024-09-06 17:54:04

这是一个老问题,但如果有人看到这个,您可以使用分数重复计数来结束所需的状态。如果您从 A 到 B 进行动画处理并使用自动反转,则每次重复将如下所示: A -> B-> A. 由于您想在最后一个动画上结束于 B 而不是 A,因此只需执行此操作即可使最后一个重复仅进行到一半(到 B):

[UIView setAnimationRepeatCount:10.5];

就是这样!

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!

伤感在游骋 2024-09-06 17:54:04

这是一个“功能”,而不是一个错误。这真的很烦人,但看起来除非苹果另有说明,否则 UIView 动画应该是这样工作的。

请参阅此答案:setAnimationRepeatAutoreverses 的行为不符合我的预期

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

╰ゝ天使的微笑 2024-09-06 17:54:04

您可以将 setAnimationDidStopSelector 设置为自定义函数,并且在自定义函数中您可以设置更多动画(甚至递归)。请参阅我对另一个问题的回答此处

通常我会定义一个动画结构(示例中的AnimationPath)来协调整个动画(例如,通过计数器)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文