繁体   English   中英

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

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

我需要翻转卡片,然后再翻转回去。 我已经编写了代码来毫无困难地做到这一点。 麻烦来了,因为用户可以根据需要来回翻转此卡多次,而且效果似乎是累积的。 第二次尝试时,卡片旋转得像陀螺一样。 我并不完全惊讶,因为似乎只有一种添加动画的方法,而不能从视图中清除动画。 有没有一种方法可以“重置”我以前提交的任何动画的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;
}

谢谢阅读。

要取消所有正在进行的动画:

#import <QuartzCore/QuartzCore.h>

...

[view.layer removeAllAnimations];

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

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

... and the rest of it ...

[UIView commitAnimations];

希望其中之一可以解决您的问题。

无论您的“自我”是什么,都将其放在一个名为foo的包含视图中,然后将setAnimation ...行更改为此:

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

暂无
暂无

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

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