Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone:动画停止选择器是否在主线程上运行?_Iphone_Objective C_Animation_Uiview - Fatal编程技术网

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

iPhone:动画停止选择器是否在主线程上运行?,iphone,objective-c,animation,uiview,Iphone,Objective C,Animation,Uiview,文档清楚地表明,动画(通过[UIView commitAnimations]调用)在单独的线程上执行。(无论如何,对我来说)不清楚的是,animationDidStopSelector是在主(UI)线程上执行的,还是在与动画相同的线程上执行的 给定以下代码: - (void) doSomethingCool { // Fade out someView [UIView beginAnimations:@"myAnimation" context:nil]; [UIView setAnima

文档清楚地表明,动画(通过[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}
在线程{name=(null),num=1}上执行beginAnimations
在线程{name=(null),num=1}上执行animationDone

…这让我想知道我的车祸到底在哪里P

看起来它们是在主线程上执行的,通过几个位置良好的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}
在线程{name=(null),num=1}上执行beginAnimations
在线程{name=(null),num=1}上执行animationDone

…这让我想知道我的车祸到底在哪里P

这是与你自己的对话!:)但是我想说的是:我会在你的停止块中放置一个断点,然后查看调试程序中的线程,这是与你自己的对话!:)但我想说的是:我会在停止块中放置一个断点,然后查看调试器中的线程