iPhone:是否停止动画选择在主线程上运行? [英] iPhone: does animation stop selector run on the main thread?

查看:173
本文介绍了iPhone:是否停止动画选择在主线程上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的文档上的事实,动画(与调用[UIView的commitAnimations])在一个单独的线程中执行清楚。什么是不明确的(对我来说,反正)是animationDidStopSelector是否在主(UI)线程或同一个线程中执行的动画。

The documentation is clear on the fact that animations (invoked with [UIView commitAnimations]) execute on a separate thread. What's not clear (to me, anyway) is whether the animationDidStopSelector is executed on the main (UI) thread or the same thread as the animation.

由于以下code:

- (void) doSomethingCool {
 // Fade out someView
 [UIView beginAnimations:@"myAnimation" context:nil];
 [UIView setAnimationDelegate:self];
 [UIView setAnimationDidStopSelector:@selector(animationDone:finished:context:)];
 someView.alpha = 0.0;
 [UIView commitAnimations];
}

- (void) animationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
 if( [animationID isEqual:@"myAnimation"] ) {
  [someView removeFromSuperview];
 }
}

我听说在除主线程以外的其他线程访问UI元素是普遍不好,因此,如果animationDone调用发生在不同的线程,那么上面的code会做坏事,是吗?

I've heard that accessing UI elements on a thread other than the main thread is "generally bad", so if the animationDone call happens on a different thread, then the above code would do bad things, yes?

这似乎并没有做坏事。但我一直在追逐一个看似随意的偶然碰撞而这部动画后会发生的,我想知道,如果有一些线程问题。

It seems not to do bad things. But I have been chasing a seemingly random occasional crash which happens after this animation, and I'm wondering if there's some threading issue.

推荐答案

看来他们的在主线程上执行,因为通过几个精心设计的NSLog的证明要求。

It appears that they are executed on the main thread, as proven via a few well-placed NSLog calls.

- (void) doSomethingCool {
    // Fade out someView
    NSLog( @"executing beginAnimations on thread %@", [NSThread currentThread] );
    [UIView beginAnimations:@"myAnimation" context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDone:finished:context:)];
    someView.alpha = 0.0;
    [UIView commitAnimations];
}

- (void) animationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    NSLog( @"executing animationDone on thread %@", [NSThread currentThread] );
    if( [animationID isEqual:@"myAnimation"] ) {
        [someView removeFromSuperview];
    }
}

...它输出:

… which outputs:

executing beginAnimations on thread <NSThread: 0x6b10860>{name = (null), num = 1}
executing animationDone on thread <NSThread: 0x6b10860>{name = (null), num = 1}

...这使我想知道我的死机确实是。 :P

...which makes me wonder where my crash really is. :P

这篇关于iPhone:是否停止动画选择在主线程上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆