2010-08-11 4 views
1

La documentation est claire sur le fait que les animations (appelées avec [UIView commitAnimations]) s'exécutent sur un thread séparé. Ce qui n'est pas clair (pour moi, de toute façon) c'est si l'animationDidStopSelector est exécutée sur le thread principal (UI) ou sur le même thread que l'animation.iPhone: le sélecteur d'animation s'arrête-t-il sur le fil principal?

Vu le code suivant:

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

J'ai entendu dire que l'accès à des éléments de l'interface utilisateur sur un autre thread que le thread principal est « généralement mauvais », donc si l'appel animationDone se produit sur un thread différent, le code ci-dessus ferait de mauvaises choses, oui?

Il semble ne pas faire de mauvaises choses. Mais j'ai été à la poursuite d'un accident occasionnel apparemment aléatoire qui se produit après cette animation, et je me demande s'il y a un problème de threading.

Répondre

1

Il semble qu'ils sont exécuté sur le thread principal, comme le prouve via quelques appels NSLog bien placés.

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

... qui sort:

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

... ce qui me fait me demander où mon accident est vraiment. : P

+0

C'est une conversation avec vous-même! :) mais je voulais dire: j'aurais juste mis un point d'arrêt dans votre bloc d'arrêt et regardé le fil dans le débogueur – nielsbot

Questions connexes