2011-01-17 11 views
7

Hola en respuesta a los eventos táctiles, mi aplicación no ve la animación. Si el usuario es realmente rápido y hace otro toque incluso mientras se desarrolla la animación actual, todo se complica.Sincronización de animaciones de UIView

¿Existe una forma estándar de manejar este problema proporcionado por el marco? ¿O estoy haciendo la animación de una manera incorrecta?

Actualmente está revisando una bandera (animationInProgress) para manejar esto, pero quería saber si eso es lo que tenemos que recurrir para esto.

Aquí está el código:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 
    NSMutableArray *c = (NSMutableArray*)context; 
    UINavigationController *nc = [c objectAtIndex:0]; 
    [nc.view removeFromSuperview]; 
    animationInProgress = NO; 
} 

- (void)transitionViews:(BOOL)topToBottom { 
    if (animationInProgress) { 
     return; 
    } 
    animationInProgress = YES; 
    NSMutableArray *context = [[NSMutableArray alloc] init]; 
    [UIView beginAnimations:@"myanim" context:context]; 
    [UIView setAnimationDuration:0.7f]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

    UIViewController *from; 
    UIViewController *to; 
    if (navController3.view.superview == nil) { 
     from = navController2; 
     to = navController3; 
    } else { 
     from = navController3; 
     to = navController2; 
    } 

    int height; 
    if (topToBottom) { 
     height = -1 * from.view.bounds.size.height; 
    } else { 
     height = from.view.bounds.size.height; 
    } 

    CGAffineTransform transform = from.view.transform; 

    [UIView setAnimationsEnabled:NO]; 
    to.view.bounds = from.view.layer.bounds; 
    to.view.transform = CGAffineTransformTranslate(transform, 0, height); 
    [window addSubview:to.view]; 

    [UIView setAnimationsEnabled:YES]; 
    from.view.transform = CGAffineTransformTranslate(from.view.transform, 0, -1 * height); 
    to.view.transform = transform; 

    [context addObject:from]; 

    [UIView commitAnimations]; 

    return; 
} 

Respuesta

10

El comportamiento predeterminado de los marcos es, como se ha notado, para permitir que le pase nada al mismo tiempo si se desea.

Usar una bandera como la que está haciendo es perfectamente razonable si su objetivo es evitar que una animación específica se ejecute si ya se está ejecutando.

Si usted quiere ir "hasta un nivel" conceptual y detener cualquier eventos de toque de entrar en su aplicación en todo durante la animación, puede utilizar:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 

emparejado con un:

[[UIApplication sharedApplication] endIgnoringInteractionEvents]; 
+0

Si detiene los eventos táctiles al ingresar a su aplicación durante la animación usando begin/endIgnoringInteractionEvents, ¿qué sucede con los eventos táctiles que pueden llegar mientras está en este bloque? ¿Se guardarán en la memoria caché y se presentarán en la aplicación una vez que se ignoreInteractionEventsEvents o se descarten todos los toques (como su nombre lo sugiere)? – vance

+0

@vance: serán desechados, como su nombre indica. –