iPhone:动画停止选择器在主线程上运行吗?

时间:2010-08-11 04:10:43

标签: iphone objective-c animation uiview

文档清楚地表明动画(使用[UIView commitAnimations]调用)在单独的线程上执行。什么不清楚(对我来说,无论如何)是animationDidStopSelector是在主(UI)线程上执行还是在与动画相同的线程上执行。

给出以下代码:

- (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调用发生在另一个线程上,那么上面的代码会做坏事,是吗?

似乎没有做坏事。但是我一直在追逐一个看似偶然的偶然事件,这个事件发生在这个动画之后,我想知道是否存在一些线程问题。

1 个答案:

答案 0 :(得分:1)

看起来他们在主线程上执行了 ,这可以通过几个很好的NSLog调用来证明。

- (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];
    }
}

......输出:

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

...这让我想知道我的崩溃究竟在哪里。 :P