2012-03-29 12 views
8

Cuando selecciono una fila en una UITableView, llamo al scrollRectToVisible:animated en el GCRect del marco de la fila e inmediatamente después hago otras animaciones. Mi problema es que no sé cuándo se completó la animación de scrollRectToVisible:animated.En una UITableView, ¿cómo sé cuándo se completa scrollRectToVisible para una fila?

Mi código:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRwoAtIndexPath:indexPath]; 

    [self.tableView scrollRectToVisible:cell.frame animated:YES]; 

    //more animations here, which I'd like to start only after the previous line is finished! 
} 
+0

+1 Buena pregunta, pero me temo que la respuesta es: no sabes cuándo 'scrollRectToVisible: animated:' finaliza. – Sam

+0

La respuesta a la siguiente pregunta también puede ser útil aquí: http://stackoverflow.com/questions/7198633/how-can-i-tell-when-a-uitableview-animation-has- finished – fishinear

Respuesta

3

El protocolo UITableViewDelegate se ajusta a UIScrollViewDelegate. Puede establecer BOOL parámetro cuando se desplaza de forma manual y que comprobarlo en scrollViewDidScroll:

BOOL manualScroll; 
... 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView cellForRwoAtIndexPath:indexPath]; 

    manualScroll = YES; 
    [self.tableView scrollRectToVisible:cell.frame animated:YES]; 
} 
... 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    if (manualScroll) 
    { 
     manualScroll = NO; 
     //Do your staff 
    } 

} 

No se olvide de establecer UITableViewDelegate.

+0

Eso es bastante hacky, pero parece que funcionaría, y lo mejor que puede hacer dado el marco. ¡Gracias por la respuesta! –

+0

hay una propiedad 'dragging' en UIScrollView que hace el seguimiento de desplazamiento manual para usted –

15

me encontré con este método UIScrollViewDelegate:

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView 
{ 
    // Do your stuff. 
} 

Sólo pidió rollos animados. No llamado para desplazamientos basados ​​en el tacto. Parece que funciona bien.

+0

¡Justo lo que estaba buscando, gracias! –

5

Una forma más fácil es para encapsular el código de desplazamiento en un [...] bloque de UIView animateWith, así:

[UIView animateWithDuration:0.3f animations:^{ 
    [self.tableView scrollRectToVisible:cell.frame animated:NO]; 
} completion:^(BOOL finished) { 
    // Some completion code 
}]; 

Nota que animó == NO en el scrollRectToVisible: animada: método.

+0

Esto debería ser el anwser –

Cuestiones relacionadas