2009-10-03 5 views
1

Je voudrais avoir une animation imbriquée pour ma vue.UIView iPhone SDK animations imbriquées

J'ai un sélecteur d'arrêt d'animation qui est appelée très bien:

[UIView setAnimationDidStopSelector:@selector(growAnimationDidStop1:finished:context:)]; 

Cependant à l'intérieur de ce sélecteur que je veux faire plus d'animation et lorsque vous avez terminé un autre sélecteur appelé:

- (void)growAnimationDidStop1:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
... 
    [UIView setAnimationDidStopSelector:@selector(growAnimationDidStop2:finished:context:)]; 
... 

    [UIView commitAnimations]; 
} 

Le problème est que growAnimationDidStop2 n'est jamais appelée. Pourquoi est-ce?

Répondre

1

Oups, répondu moi-même. J'ai dû démarrer un tout nouveau contexte d'animation dans la méthode du premier arrêt

3

Vous pouvez également le faire avec blocks, et Apple recommande maintenant ceci. Considérez ce qui suit comme pseudo-code:

[UIView animateWithDuration:0.3 
         delay:0.0 
         options:UIViewAnimationCurveEaseInOut 
        animations:^{ 
        [self doAnimationOne]; 
        } completion:^(BOOL finished){ 
        [UIView animateWithDuration:0.4 
              delay:0.0 
             options:UIViewAnimationCurveEaseInOut 
             animations:^{ 
             [self doAnimationNine]; 
             } 
             completion:^(BOOL finished) { 
             [UIView animateWithDuration:0.3 
                   delay:0.0 
                  options:UIViewAnimationCurveEaseInOut 
                 animations:^{ 
                  [self doAnimationFive]; 
                 } 
                 completion:^(BOOL finished) {}]; 
             }]; 
        }]; 

Il est aussi une bonne pratique pour vos animations pour être « social », par exemple faire:

BOOL userInteractionEnabled = [self isUserInteractionEnabled]; 
[self setUserInteractionEnabled:NO]; 

BOOL animationsEnabled = [UIView areAnimationsEnabled]; 
[UIView setAnimationsEnabled:YES]; 

avant de commencer votre animation, puis dans la réalisation définitive bloc faire:

[UIView setAnimationsEnabled:animationsEnabled]; 
[self setUserInteractionEnabled:userInteractionEnabled]; 
+3

Je voulais juste ajouter un commentaire. [UIView animateWithDuration] désactive automatiquement l'interaction de l'utilisateur pendant l'animation. Afin de permettre l'interaction pendant l'animation, vous devez passer UIViewAnimationOptionAllowUserInteraction comme options d'animation. –

1

La meilleure façon de faire plusieurs animations en séquence est avec une file de blocs.

Voyez comment nettoyer le résultat est:

NSMutableArray* animationBlocks = [NSMutableArray new]; 

typedef void(^animationBlock)(BOOL); 

// getNextAnimation 
// removes the first block in the queue and returns it 
animationBlock (^getNextAnimation)() = ^{ 
    animationBlock block = (animationBlock)[animationBlocks firstObject]; 
    if (block){ 
     [animationBlocks removeObjectAtIndex:0]; 
     return block; 
    }else{ 
     return ^(BOOL finished){}; 
    } 
}; 

//add a block to our queue 
[animationBlocks addObject:^(BOOL finished){; 
    [UIView animateWithDuration:1.0 animations:^{ 
     //...animation code... 
    } completion: getNextAnimation()]; 
}]; 

//add a block to our queue 
[animationBlocks addObject:^(BOOL finished){; 
    [UIView animateWithDuration:1.0 animations:^{ 
     //...animation code... 
    } completion: getNextAnimation()]; 
}]; 

//add a block to our queue 
[animationBlocks addObject:^(BOOL finished){; 
    NSLog(@"Multi-step Animation Complete!"); 
}]; 

// execute the first block in the queue 
getNextAnimation()(YES); 

Extrait de: http://xibxor.com/objective-c/uiview-animation-without-nested-hell/