2011-12-26 20 views
0

私は24枚のカード(UIImageView)が表示されている表面の中に隠れて表示されている、彼らは12のグループ番号、あなたが1つをタッチすると、それは開いて表示されます同じカードが開いているかどうかを調べ、一致していなければ、2枚のカードを隠します。2つのビューアニメーションを1つずつ実行するにはどうすればいいですか?

これだけです。

オープンカードと非表示のカードアクションはUIViewアニメーションを使用しました。しかし、今私は問題があります。私がカードに触れたとき、それは一致があるかどうかを見つけようとします。しかし、オープンカードとクローズカードのアクションアニメーションは同時に実行されます。私が明らかになったカードの内容を見ることはできません。

私はそれを触った後でカードを開きたいのですが(0.5秒待っても)、一致していないカードを同時に閉じることができます。同時にカードを開いてカードを閉じることはできません。しかし、下の私のコードでは、最初にカードを開き、次に計算して、2枚のカードを閉じました。

@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)を使用して、府終了したならば、あなたは確認することができます。

例:

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

私はあなたのガイドに従っています。オープンカードとクローズカードのアニメーションは、1つずつではなく、同時に実行されます。 – qichunren

+0

公開カードとクローズカードアニメーションの両方で、複数のアニメーションが存在するという理由があるかもしれません。 – qichunren

+0

複雑なアニメーションを完成ブロックに入れて作成できます。 – akashivskyy

0

あなたは使用することができます彼らは順番に実行して

[UIView setAnimationDelay:delay]; 

は、すべてのアニメーションに適切な時間を遅らせます。

+0

私はあなたが私の「クローズカード」のアニメーションでこのコードを追加する意味だと思います。しかし、私は試してみましたが、オープンカードとクローズドカードアクションは同時に実行されます。 – qichunren

0

[UIViewのanimateWithDuration:0.2 アニメーション:^ {view.alpha = 0.0;} 完了:^(BOOL終了){[図removeFromSuperview]。 }];

完了ブロックで2番目のアニメーションを作成できます。

関連する問題