2011-10-06 10 views
6

Tengo el siguiente basado en bloques de animación:¿Cómo puedo reiniciar mi animación basada en bloques cuando la aplicación pasa a primer plano?

[UIView animateWithDuration:0.5f delay:0.0f 
        options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut 
        animations:^{ 
       [view.layer setTransform:CATransform3DMakeScale(1.3f, 1.3f, 1.0f)]; 
       NSLog(@"animating"); 
        }completion:^(BOOL finished){ 
         NSLog(@"Completed"); 
        }]; 

cuando la aplicación vuelve de estar en el fondo, el bloque de terminación se llama, y ​​mis animaciones no reinicia. He intentado utilizar el siguiente método delegado para reiniciar las animaciones:

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    /* 
    Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
    */ 
    [[self viewController] animate]; 
    ...... 
} 

pero esto no ha trabajado para restaurar las animaciones.

Del mismo modo, he probado los métodos establecidos en las respuestas a estas preguntas:

pero ninguna de las sugerencias se han trabajado para mi. ¿Hay alguna otra forma de reanudar animaciones UIView basadas en bloques cuando una aplicación ha regresado desde el fondo?

Respuesta

5

Un amigo figuraba el problema, necesaria para enableAnimations en la vista cuando se vuelve del fondo

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    /* 
    Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 
    */ 
    [UIView enableAnimations:YES]; 
    [[self viewController] animate]; 
    ...... 
} 

luego en la animación antes de que la necesidad de bloque a removeAllAnimations y establecer layer.transform a la Identidad

hasStarted = YES; 
    for(UIButton * button in goldenBreakOutButtons){ 
     for (UIView* view in button.subviews) { 
      if (wasStarted) { 
       [view.layer removeAllAnimations]; 
       view.layer.transform = CATransform3DIdentity; 
      } 
      if ([view isKindOfClass:[UIImageView class]]) { 
       [UIView animateWithDuration:0.5f delay:0.0f 
         options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat 
         animations:^ { 
          [view.layer setTransform:CATransform3DMakeScale(1.3f, 1.3f, 1.0f)]; 
          NSLog(@"animating"); 
         } 
         completion:^(BOOL finished){ 
          if (finished) { 
           NSLog(@"Completed"); 
          } 

         }]; 
      } 
     } 
    } 
+2

dónde demonios has encontrado '[enableAnimations UIView: SÍ]'? No veo ese método en ninguna parte :( – Nate

+2

Un poco tarde, pero pruebe [UIView setAnimationsEnabled: YES]; –

+0

Como puede ver que ya está en mi respuesta :) – CStreel

2

por favor, intente suscribirse/darse de baja en ViewController a
standart (UIApplicationWillEnterForegroundNotification)
notificación de NSNotificationCenter:




    -(void) loadView 
    { 
    ..... 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(restartAnimation)  
               name:UIApplicationWillEnterForegroundNotification 
               object:nil];  
    .... 
    } 

    - (void) viewDidUnload 
    { 
    ... 
     [[NSNotificationCenter defaultCenter] removeObserver:self 
                 name:UIApplicationWillEnterForegroundNotification 
                object:nil]; 
    .... 
    } 

    -(void) dealloc 
    { 
     [[NSNotificationCenter defaultCenter] removeObserver:self]; 
     [super dealloc]; 
    } 

    - (void) restartAnimation 
    { 
     if(self.logoImageView) 
     { 
      [logoImageView.layer removeAllAnimations]; 

      CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 

      animation.fromValue  = [NSNumber numberWithFloat:1.0]; 
      animation.toValue   = [NSNumber numberWithFloat:0.6]; 
      animation.duration  = 1.5f; 
      animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
      animation.autoreverses = YES; 
      animation.repeatCount  = HUGE_VALF; 

      [[logoImageView layer] addAnimation:animation forKey:@"hidden"]; 
     } 
    } 

+0

ya está suscrito de forma predeterminada como UIApplicationWillEnterForegroundNotification se está llamando antes de aplicación regresando al forground. – CStreel

Cuestiones relacionadas