2011-12-26 5 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

당신은 블록 기반 애니메이션을 사용하여이 작업을 acheive 수 있습니다

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

는 그런 다음 애니메이션이 여전히 실행 또는 BOOL 바르 (예를 들어, cardAnimationIsActive)를 사용하여 BU 완료하면 확인할 수 있습니다.

예 :

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

본인의 가이드를 따라갔습니다. 오픈 카드와 클로우 카드 애니메이션은 여전히 ​​하나씩 실행되는 것이 아니라 동시에 실행됩니다. – qichunren

+0

공개 카드와 클로즈 카드 애니메이션 모두에 하나 이상의 애니메이션이 존재할 수 있습니다. – qichunren

+0

완료 블록에 넣으면 복잡한 애니메이션을 만들 수 있습니다. – akashivskyy

0

당신은 사용할 수 있습니다 그들은 순차적으로 실행되도록

[UIView setAnimationDelay:delay]; 

모든 애니메이션을 적절한 시간을 지연 할 수 있습니다.

+0

난 당신이 내 "가까운 카드"애니메이션이 코드를 추가 의미 생각합니다. 하지만 공개 카드와 클로즈 카드 액션이 동시에 실행됩니다. – qichunren

0

[UIView의 animateWithDuration 0.2 애니메이션^{view.alpha = 0.0} 완료^(BOOL 완료) {[보기 removeFromSuperview]; }];

완료 블록에서 두 번째 애니메이션을 만들 수 있습니다.

관련 문제