iPhone - UIView 动画无法正确循环
大家好,这里遇到一个小问题,不知道我做错了什么。我正在尝试反复上下动画 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个老问题,但如果有人看到这个,您可以使用分数重复计数来结束所需的状态。如果您从 A 到 B 进行动画处理并使用自动反转,则每次重复将如下所示: A -> B-> A. 由于您想在最后一个动画上结束于 B 而不是 A,因此只需执行此操作即可使最后一个重复仅进行到一半(到 B):
就是这样!
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):
That's it!
这是一个“功能”,而不是一个错误。这真的很烦人,但看起来除非苹果另有说明,否则 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
您可以将 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).