2011-12-26 44 views
0

我做「猜猜匹配牌」的遊戲,那就是:有24卡(的UIImageView)與內部視圖中顯示的表面隱藏的窗口,他們有12組號碼,你摸一個,那麼這將是內開秀,找到是否有相同的卡片打開,如果不匹配,則兩張打開的卡片將被隱藏。如何讓兩個視圖動畫一個接一個地運行?

就在今年。

開卡和隱藏卡動作中使用的UIView動畫。但是現在我有一個問題。當我碰到卡時,它會嘗試查找是否有匹配。但是開卡和關卡動作同時執行。即使我看不清我看到的卡片內容。

我想開一個卡我摸後,再(甚至等待0.5秒),在同一時間關閉運行結束不匹配卡。不能同時打開卡和關閉卡。但在我的代碼中,我先打開了一張卡片,然後計算,然後關閉了兩張操作卡。

@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 

更新:我跟着Kashiv,而這個更新的代碼:

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




} 

但opencard和closecard動畫仍然在同一時間執行。

回答

0

您可以通過使用基於塊的動畫達致這:

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

然後你可以檢查動畫仍在執行或完成BU使用BOOL伊娃(例如cardAnimationIsActive)。

例子:

- (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

我已經跟着你的指導,opencard和closecard動畫仍然同時執行,而不是一個接一個地執行。 – qichunren 2011-12-26 16:20:58

+0

可能的原因是:無論是在公開卡和關卡動畫中都有不止一個動畫。 – qichunren 2011-12-26 16:31:29

+0

您可以通過將它們放入完成塊來創建複雜的動畫。 – akashivskyy 2011-12-26 16:35:26

0

你可以使用:

[UIView setAnimationDelay:delay]; 

延遲所有動畫正確的時間,讓他們按順序運行。

+0

我想你的意思是在我的「親密卡」動畫添加該代碼。但我試過了,開卡和關卡動作仍然同時執行。 – qichunren 2011-12-26 16:12:41

0

[UIView的animateWithDuration:0.2 動畫:^ {view.alpha = 0.0;} 完成:^(BOOL完成){[視圖removeFromSuperview]; }];

您可以製作完成塊第二動畫。

相關問題