2011-10-06 29 views
6

Ho la seguente animazione basata su blocchi:Come posso riavviare la mia animazione basata su blocchi quando l'applicazione arriva in primo piano?

[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"); 
        }]; 

quando l'applicazione ritorna dall'essere in background, il blocco di completamento si chiama, e le mie animazioni non si riavvia. Ho provato a utilizzare il seguente metodo delegate per riavviare le animazioni:

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

ma questo non ha funzionato per ripristinare le animazioni.

Allo stesso modo, ho provato gli approcci disposti nelle risposte a queste domande:

ma nessuno dei suggerimenti ci hanno lavorato per me. C'è un altro modo per riprendere le animazioni di UIView basate su blocchi quando un'applicazione è tornata dallo sfondo?

risposta

5

Un amico capito il problema, necessario per enableAnimations sulla vista quando ritorna dallo sfondo

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

poi nella prima del blocco di animazione necessità di removeAllAnimations e impostare layer.transform a Identità

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

Dove diavolo hai trovato '[enableAnimations UIView: SI]'? Non vedo questo metodo da nessuna parte :( – Nate

+2

Un po 'tardi, ma prova [UIView setAnimationsEnabled: YES]; –

+0

Come puoi vedere è già nella mia risposta :) – CStreel

2

Si prega di provare a sottoscrivere/unsubscribe in ViewController per
standart (UIApplicationWillEnterForegroundNotification) notifica
da 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

è già stato sottoscritto per impostazione predefinita come UIApplicationWillEnterForegroundNotification viene chiamato prima di applicazione che ritorna in forground. – CStreel

Problemi correlati