问题描述
我开始学习使用UIView动画。所以我写了以下几行:
I am starting to learn to use UIView animation. So I wrote the following lines:
[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(一个绿色框)向后移动了两次。到目前为止一切顺利,但是我发现在移动两次之后,绿色框最终跳到了新位置(position.x + 100.0f,position.y + 100.0f),而不是返回到原始位置(position.x,position.y)。
In this case, the UIView (a green box) moved back and fore 2 times. So far so good, BUT I found out that after the moving twice, the green box ended up jumped to the "new position" (position.x + 100.0f, position.y + 100.0f) instead of going back to the original position (position.x, position.y). That makes the animation look pretty odd (like after the box swing back to the original position caused by setAnimationRepeatAutoreverses, it jumps back to the new position in the last microsecond!)
这会使动画看起来很奇怪(就像在由setAnimationRepeatAutoreverses引起的框摆动回到原始位置之后,它在最后一个微秒内跳回到新位置!)使绿框在最后一刻不跳到新位置的最佳方法是什么?
What is the best way to make the green box NOT jump to the new position at the very last minute?
推荐答案
我已经在alpha属性上使用UIView动画存在完全相同的问题。最糟糕的部分是动画在animationDidStop:代理消息发送之前,跳到了之前的最终位置。这意味着您不能在那里手动设置原始状态。
I've had exactly the same issue with using UIView animations on the alpha property. The worst part is that the animation jumps to the "final" position before the animationDidStop: delegate message is sent, which means you can't manually set the original state there.
我的解决方案是使用animationDidStop委托消息创建一个新的动画块,并将它们全部串在一起:
My solution was to use the animationDidStop delegate messages to create a new animation block, and string them all together:
- (void)performAnimation
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(phase1AnimationDidStop:finished:context:)];
// Do phase 1 of your animations here
CGPoint center = someView.center;
center.x += 100.0;
center.y += 100.0;
someView.center = center;
[UIView commitAnimations];
}
- (void)phase1AnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(phase2AnimationDidStop:finished:context:)];
// Do phase 2 of your animations here
CGPoint center = someView.center;
center.x -= 100.0;
center.y -= 100.0;
someView.center = center;
[UIView commitAnimations];
}
- (void)phase2AnimationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
// Perform cleanup (if necessary)
}
您可以将任意多个动画块组合在一起这条路。这似乎是浪费代码的方法,但是直到Apple给我们提供-[UIView setAnimationFinishesInOriginalState:]属性或类似属性,这才是我发现解决此问题的唯一方法。
You can string together as many animation blocks as you want this way. It seems wasteful code-wise, but until Apple give us a -[UIView setAnimationFinishesInOriginalState:] property or something similar, this the only way that I've found to solve this problem.
这篇关于setAnimationRepeatAutoreverses行为不符合我的预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!