如何让两个视图动画一个接一个地运行?

时间:2011-12-26 16:02:25

标签: iphone ios uianimation

我打算制作“猜猜卡片”游戏,就是:窗口里有24张卡片(UIImageView),隐藏里面的视图显示表面,它们是一组12组,你触摸一个,然后就会打开显示在里面,查找是否有相同的卡打开,如果不匹配,则两张打开的卡将隐藏。

就是这个。

打开卡片并隐藏卡片动作使用UIView动画。但现在我有一个问题。当我碰到卡片时,它会尝试查找是否有匹配。但是开卡和关卡动作动画同时执行。即使我看不清楚我看到的卡内容是什么。

我想在触摸后打开一张卡片,然后(甚至等待0.5秒钟)同时关闭不匹配的卡片。不能同时开卡和关卡。但是在我下面的代码中,我首先打开了一张卡,然后计算,然后关闭两张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

更新:我关注了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动画仍然同时执行。

3 个答案:

答案 0 :(得分:0)

您可以使用:

[UIView setAnimationDelay:delay];

将所有动画延迟到适当的时间,以便它们按顺序运行。

答案 1 :(得分:0)

  

[UIView animateWithDuration:0.2        动画:^ {view.alpha = 0.0;}        完成:^(BOOL完成){[view removeFromSuperview]; }];

您可以在完成块中创建第二个动画。

答案 2 :(得分:0)

您可以使用基于块的动画实现此目的:

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

然后您可以使用BOOL iVar(例如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;
    }];
}