UIView 动画出现问题 - 多次执行相同的动画不起作用
我需要翻转一张卡片,然后再翻转回来。我已经编写了代码来毫无困难地做到这一点。麻烦来了,因为用户可以根据需要多次来回翻转这张卡,而且效果似乎是累积的。第二次尝试时,卡片像陀螺一样旋转。我并不完全感到惊讶,因为似乎只有一种方法可以添加动画,而从视图中看不到清晰的动画。有没有办法“重新设置”我之前提交的任何动画的 UIView?有没有一种方法可以捕获它并重新使用它而无需提交新的?或者我错过了一些明显的东西?这是我的代码:
if (self.flipped) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self
cache:YES];
[imageView removeFromSuperview];
[UIView commitAnimations];
self.flipped = NO;
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self
cache:YES];
[self addSubview:imageView];
[UIView commitAnimations];
self.flipped = YES;
}
感谢您的阅读。
I need to flip a card and then flip it back. I have written code to do that with no trouble. The trouble comes because users can flip this card back and forth as many times as they want, and it seems that the effect is cumulative. On the second try, the card spins like a top. I'm not completely surprised, as there only seems to be a way to add animation, not clear one from the view. Is there a way to 're-set' a UIView of any animations I had previously committed? Is there a way to capture it and re-use it without committing a new one? Or am I missing something obvious? This is my code:
if (self.flipped) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self
cache:YES];
[imageView removeFromSuperview];
[UIView commitAnimations];
self.flipped = NO;
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self
cache:YES];
[self addSubview:imageView];
[UIView commitAnimations];
self.flipped = YES;
}
Thanks for reading.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取消所有正在进行的动画:
启动一个新的动画,杀死现有的动画(如果有),并从当前视图所在的位置开始移动 - 我认为这就是您想要的:
希望其中一个能够解决您的问题。
To cancel all ongoing animations:
To start a new animation that kills existing animations, if any, and starts the movement from wherever the view currently is — which I think is what you want:
Hopefully one of those will solve your problem.
无论您的“自我”是什么,请将其放入名为 foo 的包含视图中,然后将 setAnimation... 行更改为:
Whatever your "self" is, put it in a containing view called foo, and then change your setAnimation... line to this: