1、2、3の数字ボタンをタップ。
よこから、黒いバー、続けて、
ひらがな、カタカナ、漢数字がポンポンポンと。
というおもちゃiPhone アプリのサンプルを作ってみました。

ポイント
UIViewのanimationWithDurationをたっぷりネストさせています。

環境
今回つくったiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています。


サンプルコード


#import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    [self createButtons];

}

– (void)createButtons

{

    for (int i = 0; i<3; i++) {

        UILabel *btn = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];

        btn.center = CGPointMake(i * 100 + 60, 200);

        btn.backgroundColor = [UIColor whiteColor];

        btn.text = [NSString stringWithFormat:@”%d”, i+1];

        btn.textColor = self.view.backgroundColor;

        btn.textAlignment = 1;

        btn.font = [UIFont systemFontOfSize:80];

        btn.tag = i;

        btn.userInteractionEnabled = YES;

        [self.view addSubview:btn];

        

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

        [btn addGestureRecognizer:tap];

    }

}

– (void)tap:(UITapGestureRecognizer*)gr

{

    UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(320, 100, 320, 170)];

    bar.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];

    [self.view addSubview:bar];

    

    UILabel *A = [[UILabel alloc] init];

    A.backgroundColor = [UIColor clearColor];

    A.textAlignment = 1;

    A.font = [UIFont boldSystemFontOfSize:60];

    A.textColor = [UIColor redColor];

    A.center = CGPointMake(80, 160);

    A.transform = CGAffineTransformMakeTranslation(320, 0);

    

    UILabel *B = [[UILabel alloc] init];

    B.backgroundColor = [UIColor clearColor];

    B.textAlignment = 1;

    B.font = A.font;

    B.textColor = [UIColor redColor];

    B.center = CGPointMake(170, 200);

    B.transform = CGAffineTransformMakeTranslation(320, 0);

    

    UILabel *C = [[UILabel alloc] init];

    C.backgroundColor = [UIColor clearColor];

    C.textAlignment = 1;

    C.font = A.font;

    C.textColor = [UIColor redColor];

    C.center = CGPointMake(260, 240);

    C.transform = CGAffineTransformMakeTranslation(320, 0);

    

    [self.view addSubview:A];

    [self.view addSubview:B];

    [self.view addSubview:C];

    

    if (gr.view.tag == 0) {

        A.text = @”いち;

        B.text = @”イチ;

        C.text = @”;

    } else if (gr.view.tag == 1) {

        A.text = @”;

        B.text = @”;

        C.text = @”;

    } else if (gr.view.tag == 2) {

        A.text = @”さん;

        B.text = @”サン;

        C.text = @”;

    }

    

    [A sizeToFit];

    [B sizeToFit];

    [C sizeToFit];

    

    [UIView animateWithDuration:0.2 animations:^{

        bar.transform = CGAffineTransformMakeTranslation(-320, –0);

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.2 animations:^{

            A.transform = CGAffineTransformIdentity;

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:0.2 animations:^{

                B.transform = CGAffineTransformIdentity;

            } completion:^(BOOL finished) {

                [UIView animateWithDuration:0.2 animations:^{

                    C.transform = CGAffineTransformIdentity;

                } completion:^(BOOL finished) {

                    [UIView animateWithDuration:0.2 animations:^{

                        A.transform = CGAffineTransformMakeTranslation(-320, 0);

                        B.transform = CGAffineTransformMakeTranslation(-320, 0);

                        C.transform = CGAffineTransformMakeTranslation(-320, 0);

                    } completion:^(BOOL finished) {

                        [UIView animateWithDuration:0.2 animations:^{

                            bar.transform = CGAffineTransformMakeTranslation(-640, 0);

                        } completion:^(BOOL finished) {

                            [bar removeFromSuperview];

                            [A removeFromSuperview];

                            [B removeFromSuperview];

                            [C removeFromSuperview];

                        }];

                    }];

                }];

            }];

        }];

    }];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end