2011-12-26 1 views
0

Je dois faire "Guess match carte" jeu, c'est: il y a 24 cartes (UIImageView) dans les fenêtres avec masquer à l'intérieur vue surface affichée, Ils un numéro 12 groupes, vous touchez un, puis il sera ouvert à l'intérieur , trouvez s'il y a une même carte ouverte, si elle ne correspond pas, deux cartes ouvertes seront alors cachées.Comment faire en sorte que deux animations de vue s'exécutent une par une?

Juste ceci.

Ouvrir la carte et masquer l'action de la carte utilisée UIView animation. Mais maintenant j'ai un problème. Quand je touche la carte, j'essaye de trouver s'il y a un match. Mais l'animation d'une carte ouverte et d'une action de fermeture de carte s'exécute en même temps. même je ne peux pas voir quel contenu de la carte je vois clairement.

Je veux ouvrir une carte après l'avoir touchée, puis (même attendre 0,5 seconde) fermer les cartes opend non appariées en même temps. Ne pas ouvrir la carte et fermer la carte en même temps. Mais dans mon code ci-dessous, j'ai d'abord ouvert une carte, puis j'ai calculé et fermé deux cartes opend.

@interface Card : UIImageView 

@property BOOL expanded; 
@property BOOL found; 
@property (retain)NSString * nameTitle; 
@property (retain) UIImage * expandedImage; 

@end 


// 
// Card.m 
// Guest card match 
// 
// Created by on 11-10-20. 
// Copyright 2011年 __MyCompanyName__. All rights reserved. 
// 

#import "Card.h" 
#import "MainAppDelegate.h" 

@implementation Card 
@synthesize expanded; 
@synthesize found; 
@synthesize expandedImage; 
@synthesize nameTitle; 

- (id)init 
{ 
    if ((self = [super init])) { 
     [self setUserInteractionEnabled:YES]; 
     self.image = [UIImage imageNamed:@"cardface_48.png"]; 
     self.expanded = NO; 
     self.found = NO; 
    } 
    return self; 
} 

- (void)openCard{ 
    NSLog(@"open card"); 
    if (self.expanded){return;} 
    self.expanded = YES; 
    [UIView beginAnimations:@"animation1" context:nil]; 
    [UIView setAnimationDuration:0.8]; 
    [UIView setAnimationDelegate:self]; 

    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES]; 
    [self setTransform:CGAffineTransformMakeScale(3.0, 3.0)]; 
    [self setImage:self.expandedImage]; 
    [UIView commitAnimations]; 

    [UIView beginAnimations:@"animation1_open" context:nil]; 
    [UIView setAnimationDuration:1.2]; 
    [UIView setAnimationDelegate:self]; 

    [UIView setAnimationBeginsFromCurrentState:NO]; 
    [self setTransform:CGAffineTransformMakeScale(1, 1)]; 
    [UIView commitAnimations]; 


} 

- (void)closeCard{ 
    if (!self.expanded){return;} 
    self.expanded = NO; 

    [UIView beginAnimations:@"animation1_close" context:nil]; 
    [UIView setAnimationDuration:0.8]; 
    [UIView setAnimationDelegate:self]; 


    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES]; 
    [self setTransform:CGAffineTransformMakeScale(3.0, 3.0)]; 
    [self setImage:[UIImage imageNamed:@"cardface_48.png"]]; 
    [UIView commitAnimations]; 

    [UIView beginAnimations:@"animation2_close" context:nil]; 
    [UIView setAnimationDuration:1.2]; 
    [UIView setAnimationDelegate:self]; 


    [UIView setAnimationBeginsFromCurrentState:NO]; 
    [self setTransform:CGAffineTransformMakeScale(1, 1)]; 
    [UIView commitAnimations]; 
    [self setUserInteractionEnabled:YES]; 


} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    // Do what you want here 
    //NSLog(@"touchesBegan!"); 
    //[self setUserInteractionEnabled:NO]; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    NSLog(@"card tag: %d", self.tag); 
    if (self.expanded) { 
     return; 
    } 

    [self openCard]; 
    for (NSInteger tagNumber=10001; tagNumber<10025; tagNumber++) { 
     Card *card = (Card *)[self.superview viewWithTag:tagNumber]; 
     if (card.expanded && card.tag != self.tag && !card.found) { 
      if ([card.nameTitle isEqualToString:self.nameTitle]) {// Found match! 
       NSLog(@"Match!"); 
       [card setUserInteractionEnabled:NO]; 
       [self setUserInteractionEnabled:NO]; 
       card.found = YES; 
       self.found = YES; 
      }else{ 
       NSLog(@"not Match!"); 
       [card closeCard]; 
       [self closeCard]; 

      } 
     }else{ 
      [self setUserInteractionEnabled:YES]; 
     } 
    } 


} 

@end 

Mise à jour: J'ai suivi Kashiv, et ce code mis à jour:

- (void)openCard{ 
    NSLog(@"open card"); 
    if(cardAnimationIsActive) return; 
    cardAnimationIsActive = YES; 


    if (self.expanded){return;} 
    self.expanded = YES; 


    [UIView animateWithDuration:2.0f animations:^{ 


     [UIView beginAnimations:@"animation1" context:nil]; 
     [UIView setAnimationDuration:0.8]; 
     [UIView setAnimationDelegate:self]; 

     [UIView setAnimationBeginsFromCurrentState:YES]; 
     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES]; 
     [self setTransform:CGAffineTransformMakeScale(3.0, 3.0)]; 
     [self setImage:self.expandedImage]; 
     [UIView commitAnimations]; 

     [UIView beginAnimations:@"animation1_open" context:nil]; 
     [UIView setAnimationDuration:1.2]; 
     [UIView setAnimationDelegate:self]; 

     [UIView setAnimationBeginsFromCurrentState:NO]; 
     [self setTransform:CGAffineTransformMakeScale(1, 1)]; 
     [UIView commitAnimations]; 



    } completion:^(BOOL finished){ 
     cardAnimationIsActive = NO; 
    }]; 




} 

- (void)closeCard{ 

    if(cardAnimationIsActive) return; 
    cardAnimationIsActive = YES; 

    if (!self.expanded){return;} 
    self.expanded = NO; 

    [UIView animateWithDuration:5.0f animations:^{ 
     [UIView beginAnimations:@"animation1_close" context:nil]; 
     [UIView setAnimationDuration:0.8]; 
     [UIView setAnimationDelegate:self]; 


     [UIView setAnimationBeginsFromCurrentState:YES]; 
     [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES]; 
     [self setTransform:CGAffineTransformMakeScale(3.0, 3.0)]; 
     [self setImage:[UIImage imageNamed:@"android_48.png"]]; 
     [UIView commitAnimations]; 

     [UIView beginAnimations:@"animation2_close" context:nil]; 
     [UIView setAnimationDuration:1.2]; 
     [UIView setAnimationDelegate:self]; 


     [UIView setAnimationBeginsFromCurrentState:NO]; 
     [self setTransform:CGAffineTransformMakeScale(1, 1)]; 
     [UIView commitAnimations]; 
     [self setUserInteractionEnabled:YES]; 
    } completion:^(BOOL finished){ 
     cardAnimationIsActive = NO; 
    }]; 




} 

Mais Opencard et animation closecard exécutent toujours en même temps.

Répondre

0

Vous pouvez acheive cela en utilisant l'animation à base de blocs:

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion 

Ensuite, vous pouvez vérifier si l'animation est en cours d'exécution ou encore bu est terminé en utilisant BOOL iVar (par exemple cardAnimationIsActive).

Exemple:

- (void)openCard { 
    if(cardAnimationIsActive) return; 
    cardAnimationIsActive = YES; 
    [UIView animateWithDuration:duration animations:^{ 
     --- your open animations --- 
    } completion:^(BOOL finished){ 
     cardAnimationIsActive = NO; 
    }]; 
} 

- (void)closeCard { 
    if(cardAnimationIsActive) return; 
    cardAnimationIsActive = YES; 
    [UIView animateWithDuration:duration animations:^{ 
     --- your close animations --- 
    } completion:^(BOOL finished){ 
     cardAnimationIsActive = NO; 
    }]; 
} 
+0

J'ai suivi votre guide, l'animation opencard et closecard s'exécute toujours en même temps, pas un par un. – qichunren

+0

Peut être la raison de prblem est: à la fois dans la carte ouverte et fermer l'animation de la carte il y a plus d'une animation. – qichunren

+0

Vous pouvez créer des animations complexes en les plaçant dans le bloc d'achèvement. – akashivskyy

0

Vous pouvez utiliser:

[UIView setAnimationDelay:delay]; 

pour retarder toutes les animations en temps voulu afin qu'ils fonctionnent en séquence.

+0

Je pense que vous voulez dire ajouter ce code dans mon animation "carte close". Mais j'ai essayé, la carte ouverte et l'action de fermeture de carte s'exécutent toujours en même temps. – qichunren

0

[UIView animateWithDuration: 0.2 animations:^{view.alpha = 0,0;} achèvement:^(BOOL terminé) {[voir removeFromSuperview]; }];

Vous pouvez créer une deuxième animation dans le bloc d'achèvement.

Questions connexes