iPhoneのっけた数

小さい四角を真ん中の大きな四角の上に何個のっけたかを数えて遊ぶような子供向けiPhoneアプリを書いてみます。


動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (strong, nonatomic) UIView *area;

@property (strong, nonatomic) UILabel *counterA;

@property (strong, nonatomic) UILabel *counterB;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [self color:4];

    [self createArea];

    [self createChips];

    [self createCounter];

}

– (void)createArea

{

    self.area = [[UIView alloc] initWithFrame:CGRectMake(100, 10, 368, 220)];

    self.area.backgroundColor = [self color:3];

    self.area.layer.borderColor = [self color:2].CGColor;

    self.area.layer.borderWidth = 10;

    [self.view addSubview:self.area];

}

– (void)createChips

{

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

        float x = i * 50 + 40;

        float y = 250;

        UIView *chip = [[UIView alloc] initWithFrame:CGRectMake(x, y, 40, 40)];

        chip.backgroundColor = [self color:(i / 5)];

        chip.layer.shadowColor = [UIColor blackColor].CGColor;

        chip.layer.shadowOffset = CGSizeMake(3, 3);

        chip.layer.shadowRadius = 2;

        chip.layer.shadowOpacity = 0.5;

        

        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];

        [chip addGestureRecognizer:pan];

        

        [self.view addSubview:chip];

    }

}

– (void)createCounter

{

    self.counterA = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 70, 70)];

    self.counterA.backgroundColor = [self color:0];

    self.counterA.text = @”0″;

    self.counterA.textColor = [UIColor whiteColor];

    self.counterA.textAlignment = NSTextAlignmentCenter;

    self.counterA.font = [UIFont boldSystemFontOfSize:50];

    [self.view addSubview:self.counterA];

    

    self.counterB = [[UILabel alloc] initWithFrame:CGRectMake(480, 80, 70, 70)];

    self.counterB.backgroundColor = [self color:1];

    self.counterB.text = @”0″;

    self.counterB.textColor = [UIColor whiteColor];

    self.counterB.textAlignment = NSTextAlignmentCenter;

    self.counterB.font = [UIFont boldSystemFontOfSize:50];

    [self.view addSubview:self.counterB];

}

– (void)move:(UIPanGestureRecognizer*)gr

{

    CGPoint p = [gr locationInView:self.view];

    gr.view.center = p;

    

    if (gr.state == UIGestureRecognizerStateEnded) {

        // count

        int countA = 0;

        int countB = 0;

        for (UIView *v in self.view.subviews) {

            if (CGRectContainsRect(self.area.frame, v.frame)) {

                if ([v.backgroundColor isEqual:[self color:0]]) {

                    countA++;

                } else if ([v.backgroundColor isEqual:[self color:1]]) {

                    countB++;

                }

            }

        }

        self.counterA.text = [NSString stringWithFormat:@”%d”, countA];

        self.counterB.text = [NSString stringWithFormat:@”%d”, countB];

    }

}

#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*)color:(int)i

{

    switch (i) {

        case 0:

            return UIColorHex(0xEEE32B);

        case 1:

            return UIColorHex(0x242A40);

        case 2:

            return UIColorHex(0x9AFFFE);

        case 3:

            return UIColorHex(0x3DABE8);

        case 4:

            return UIColorHex(0xCBF8FF);

        default:

            break;

    }

    return nil;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end