简体   繁体   English

UIView动画出现问题-一次执行同一动画不起作用

[英]Trouble with UIView Animations - performing the same animation more than once doesn't work

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? 有没有一种方法可以“重置”我以前提交的任何动画的UIView? 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. 谢谢阅读。

To cancel all ongoing animations: 要取消所有正在进行的动画:

#import <QuartzCore/QuartzCore.h>

...

[view.layer removeAllAnimations];

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: 要启动一个新动画以杀死现有动画(如果有的话),并从当前视图所在的位置开始移动,我想这就是您想要的:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];

... and the rest of it ...

[UIView commitAnimations];

Hopefully one of those will solve your problem. 希望其中之一可以解决您的问题。

Whatever your "self" is, put it in a containing view called foo, and then change your setAnimation... line to this: 无论您的“自我”是什么,都将其放在一个名为foo的包含视图中,然后将setAnimation ...行更改为此:

    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                       forView:foo
                         cache:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM