我开始学习使用 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/