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

发布于 2024-11-08 21:06:21 字数 947 浏览 0 评论 0原文

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情痴 2024-11-15 21:06:21

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

#import <QuartzCore/QuartzCore.h>

...

[view.layer removeAllAnimations];

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

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

... and the rest of it ...

[UIView commitAnimations];

希望其中一个能够解决您的问题。

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.

金兰素衣 2024-11-15 21:06:21

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

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

Whatever your "self" is, put it in a containing view called foo, and then change your setAnimation... line to this:

    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                       forView:foo
                         cache:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文