iphone - setAnimationRepeatAutoreverses 的行为不符合我的预期

标签 iphone core-animation

我开始学习使用 UIView 动画。所以我写了以下几行:

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

在本例中,UIView(绿色框)前后移动了 2 次。到目前为止一切顺利,但我发现移动两次后,绿色框最终跳到“新位置”(position.x + 100.0f,position.y + 100.0f),而不是回到原始位置(位置.x,位置.y)。这使得动画看起来很奇怪(就像在 setAnimationRepeatAutoreverses 导致盒子摆动回原始位置后,它在最后一微秒跳回新位置!)

让绿色框在最后一刻不跳到新位置的最佳方法是什么?

最佳答案

我发现使用偶数 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;

关于iphone - setAnimationRepeatAutoreverses 的行为不符合我的预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2516184/

相关文章:

iphone - 如何防止在 iPhone 3GS 上安装

iphone - 删除 CAKeyframeAnimation 不会释放内存

iphone - 在 CATransaction 中对自定义 CALayer 属性进行动画处理

iphone - 出现时放大网页 View

iphone - 第一个和最后一个 UITableViewCell 在滚动时不断变化

iphone - 从客户端发送推送通知

cocoa - CoreAnimation 不适用于 QTMovieView 上的 -setAlphaValue

iphone - 如何在 Cocoa 中将一张图像与另一张图像的 alpha 值相乘?

iphone - 在 UITableView 中以编程方式移动带动画的行

ios - 移除时反转 CABasicAnimation