0から9のすうじを足してみよう。
まちがえると食べられちゃうぞ!
という感じで、iPhone用、たし算アプリの作り方を書いてみる。

ポイント
UILabelを回答用に10枚、問題用に2枚配置しています。
問題用のラベルに数字を設定するタイミングで、
こたえを保持、回答の数字が押されたときに比較して
結果に応じたアニメーション、正解なら次の問題を表示、
不正解なら、口を閉じるような動きをさせています。

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

iPhone足し算アプリの作り方

サンプルコード

#import “ViewController.h”

@interface ViewController () {

    int answer;

    UILabel *left;

    UILabel *right;

    NSMutableArray *panels;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [self getColorAtIndex:0];

    

    [self createQuestionBox];

    [self createAnswerPanel];

    

    [self setQuestion];

}

– (void)createQuestionBox

{

    left = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    left.font = [UIFont systemFontOfSize:80];

    left.textAlignment = 1;

    left.text = @”0″;

    left.backgroundColor = [self getColorAtIndex:1];

    left.center = CGPointMake(60, 160);

    left.textColor = [self getColorAtIndex:2];

    [self.view addSubview:left];

    

    UILabel *plus = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    plus.font = [UIFont systemFontOfSize:80];

    plus.center = CGPointMake(160, 160);

    plus.backgroundColor = [UIColor clearColor];

    plus.textColor = [self getColorAtIndex:2];

    plus.textAlignment = 1;

    plus.text = @”+”;

    [self.view addSubview:plus];

    

    right = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    right.font = [UIFont systemFontOfSize:80];

    right.center = CGPointMake(260, 160);

    right.textColor = [self getColorAtIndex:2];

    right.textAlignment = 1;

    right.text = @”0″;

    right.backgroundColor = [self getColorAtIndex:4];

    [self.view addSubview:right];

}

– (void)createAnswerPanel

{

    panels = [[NSMutableArray alloc] init];

    

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

        

        float x = (i % 5) * 60 + 20;

        float y = (i / 5) * 100 + 300;

        

        UILabel *ans = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 40, 40)];

        ans.font = [UIFont systemFontOfSize:30];

        ans.textColor = [self getColorAtIndex:3];

        ans.textAlignment = 1;

        ans.text = [NSString stringWithFormat:@”%d”, i];

        ans.backgroundColor = [self getColorAtIndex:2];

        

        [self.view addSubview:ans];

        [panels addObject:ans];

        

        ans.userInteractionEnabled = YES;

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

        [ans addGestureRecognizer:tap];

    }

}

– (void)setQuestion

{

    answer = arc4random() % 9;

    int leftNum = arc4random() % (answer + 1);

    int rightNum = answer – leftNum;

    

    left.text = [NSString stringWithFormat:@”%d”, leftNum];

    right.text = [NSString stringWithFormat:@”%d”, rightNum];

}

– (void)tap:(UITapGestureRecognizer*)gr

{

    if (answer == [((UILabel*)gr.view).text intValue]) {

        

        [UIView animateWithDuration:0.5 animations:^{

            gr.view.transform = CGAffineTransformMakeScale(1.5, 1.5);

            left.transform = CGAffineTransformMakeTranslation(-200, 0);

            right.transform = CGAffineTransformMakeTranslation(200, 0);

        } completion:^(BOOL finished) {

           

            [self setQuestion];

            

            [UIView animateWithDuration:0.5 animations:^{

                gr.view.transform = CGAffineTransformIdentity;

                left.transform = CGAffineTransformIdentity;

                right.transform = CGAffineTransformIdentity;

            }];

            

        }];

    } else {

        for (UIView *v in panels) {

            [UIView animateWithDuration:0.5 animations:^{

                v.transform = CGAffineTransformMakeTranslation(0 , 370 – v.center.y);

            } completion:^(BOOL finished) {

                [UIView animateWithDuration:0.5 animations:^{

                    v.transform = CGAffineTransformIdentity;

                }];

            }];

        }

        

    }

    

}

#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

– (UIColor*)getColorAtIndex:(int)i

{

    switch (i) {

        case 0:

            return UIColorHex(0x6251E8);

            break;

        case 1:

            return UIColorHex(0x32FFC8);

            break;

        case 2:

            return UIColorHex(0xFF3F4B);

            break;

        case 3:

            return UIColorHex(0xE8BD61);

            break;

        default:

            return UIColorHex(0xCAFFB4);

            break;

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end