UIView animateWithDuration:delay: 工作很奇怪

发布于 2024-10-01 06:43:29 字数 785 浏览 1 评论 0原文

我在使用 iPhone 动画块时遇到了一个奇怪的问题。 此代码:

[UIView animateWithDuration:2 delay: 0 options: 0 animations:
^(void) { [controller setBackgroundColor: [UIColor blueColor]]; }
completion: nil];

[UIView animateWithDuration:2 delay: 2 options: 0 animations:
^(void) { [controller setBackgroundColor: [UIColor redColor]]; }
completion: nil];

立即将背景设置为红色,而不传递蓝色状态。 即使对于此代码也是如此:

[UIView animateWithDuration:2 delay: 0 options: 0 animations:
^(void) { [controller setBackgroundColor: [UIColor blueColor]]; }
completion: 

^(BOOL wrwg) {
    [UIView animateWithDuration:2 delay: 2 options: 0 animations:
    ^(void) { [controller setBackgroundColor: [UIColor redColor]]; }
    completion: nil];
}];

也就是说,我尝试在第一个动画完成后运行第二个动画。 有什么问题吗?

I experience a strange problem with iPhone animation blocks.
This code:

[UIView animateWithDuration:2 delay: 0 options: 0 animations:
^(void) { [controller setBackgroundColor: [UIColor blueColor]]; }
completion: nil];

[UIView animateWithDuration:2 delay: 2 options: 0 animations:
^(void) { [controller setBackgroundColor: [UIColor redColor]]; }
completion: nil];

Instantly sets the background to red, without passing the blue state.
The same thing EVEN for this code:

[UIView animateWithDuration:2 delay: 0 options: 0 animations:
^(void) { [controller setBackgroundColor: [UIColor blueColor]]; }
completion: 

^(BOOL wrwg) {
    [UIView animateWithDuration:2 delay: 2 options: 0 animations:
    ^(void) { [controller setBackgroundColor: [UIColor redColor]]; }
    completion: nil];
}];

That is, where I try to run the second animation AFTER the first has finished.
What's the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

东京女 2024-10-08 06:43:30

我也有同样的问题。确保控制器的类型为 UIView,否则动画块将被解释为“没有要执行的动画”,因此会跳过并渲染下一个动画。

另请参阅这篇文章,我在其中提出了同样的问题(以及我的解决方案):
http://www.iphonedevsdk .com/forum/iphone-sdk-development/64451-animation-one-after-another.html#post265531

I had the same problem. Make shure that controller is of type UIView otherwise the animation block is interpreted as "no animation to perform" and therefore skipped and the next animation is rendered.

Also have look to this post where I asked the same question (with solution by me):
http://www.iphonedevsdk.com/forum/iphone-sdk-development/64451-animation-one-after-another.html#post265531

手长情犹 2024-10-08 06:43:30

属性 backgroundColor 在 UIView 中是动画的。相反,请使用视图层的 backgroundColor 属性:

#import <QuartzCore/QuartzCore.h> // don't forget to link this library in project settings

[UIView animateWithDuration:2.0
                      delay:0.0
                    options:UIViewAnimatingOptionBeginFromCurrentState
                 animations:^(void){
                   controller.layer.backgroundColor = [UIColor blueColor].CGColor;
                 }
                 completion:NULL];

PS 正如之前的文章中指出的那样,请确保您的 controller 继承自 UIView。如果它继承自 UIViewController,请改用 controller.view

Property backgroundColor is NOT animated in UIView. Instead, use backgroundColor property of the view's layer:

#import <QuartzCore/QuartzCore.h> // don't forget to link this library in project settings

[UIView animateWithDuration:2.0
                      delay:0.0
                    options:UIViewAnimatingOptionBeginFromCurrentState
                 animations:^(void){
                   controller.layer.backgroundColor = [UIColor blueColor].CGColor;
                 }
                 completion:NULL];

P.S. As was pointed in previous posts, make sure that your controller is inherited from UIView. If it is inherited from UIViewController, use controller.view instead.

划一舟意中人 2024-10-08 06:43:30

我刚刚遇到了这个问题,而且我经常遇到这个问题。有两个原因,尽管其中之一是我尚未测试的。

原因一,我忘记将界面生成器连接到动画视图,因此块显示“Animate on nil?没有什么可做的,所以我要跳到完成块!”

另一个原因是,有一些事情要做,但它不是一个可动画的属性(隐藏),我敢打赌,一旦动画中的变量匹配它们的“to”或“end”状态,就会调用完成块。这只是一个理论,但我敢打赌它是一个单独的线程,以一定的速度执行:
if (currentValueOfProperty = FinalValue) doCompletion()。 (请原谅伪代码)。

不过,也许有人应该测试一下

I just had this issue, and I commonly have it. There are two causes, although one is something I have yet to test.

Cause one, I forgot to hook up interface builder to the animated view, thus the block said "Animate on nil? There's nothing to do, so I'm gonna skip to the completion block!"

The other cause is, there is something to do, but it's not an animatable property (Hidden), and I would bet that the completion block is called as soon as the variables in the animation match their "to" or "end" state. It's just a theory, but I'm betting it's a separate thread that executes at a certain pace with:
if (currentValueOfProperty = finalValue) doCompletion(). (pardon the pseudo code).

Somebody should probably test that, though

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