繁体   English   中英

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

[英]iPhone: does animation stop selector run on the main thread?

关于动画(由[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调用发生在另一个线程上,那么上面的代码会做不好的事情,是吗?

似乎没有做坏事。 但是我一直在追寻看似随机的偶然崩溃,该崩溃在此动画之后发生,我想知道是否存在一些线程问题。

似乎它们在主线程执行的,这已通过一些适当放置的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

暂无
暂无

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

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