iPhone数字

数字の書いてあるカードを右にスワイプで足し算、左にスワイプで引き算、というiPhoneアプリを描いてみます。


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

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (strong, nonatomic) UILabel *label;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];

    

    [self createLabel];

    

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

        [self createCard];

    }

}

– (void)createLabel

{

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, 220, 50)];

    self.label.backgroundColor = [UIColor clearColor];

    self.label.textColor = [UIColor whiteColor];

    self.label.text = @”0000″;

    self.label.textAlignment = NSTextAlignmentCenter;

    self.label.font = [UIFont boldSystemFontOfSize:40];

    [self.view addSubview:self.label];

}

– (void)createCard

{

    int number = arc4random() % 20;

    

    float x = (arc4random() % 20) * 12 + 40;

    float y = (arc4random() % 30) * 10 + 150;

    

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

    card.text = [NSString stringWithFormat:@”%d”, number];

    card.textAlignment = NSTextAlignmentCenter;

    card.font = [UIFont boldSystemFontOfSize:30];

    card.textColor = [[UIColor greenColor] colorWithAlphaComponent:0.8];

    

    card.layer.borderColor = [UIColor orangeColor].CGColor;

    card.layer.borderWidth = 3;

    

    card.center = CGPointMake(x, y);

    card.layer.masksToBounds = NO;

    card.layer.shadowOffset = CGSizeMake(-10, 16);

    card.layer.shadowRadius = 8;

    card.layer.shadowOpacity = 0.4;

    [self.view addSubview:card];

    

    card.userInteractionEnabled = YES;

    

    UISwipeGestureRecognizer *right = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(plus:)];

    right.direction = UISwipeGestureRecognizerDirectionRight;

    [card addGestureRecognizer:right];

    

    UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(minus:)];

    left.direction = UISwipeGestureRecognizerDirectionLeft;

    [card addGestureRecognizer:left];

}

– (void)plus:(UISwipeGestureRecognizer*)gr

{

    UILabel *l = (UILabel*)gr.view;

    int selected = [l.text intValue];

    int current = [self.label.text intValue];

    self.label.text = [NSString stringWithFormat:@”%04d”, selected + current];

    

    [UIView animateWithDuration:0.6 animations:^{

        gr.view.transform = CGAffineTransformMakeTranslation(320, 0);

    } completion:^(BOOL finished) {

        [gr.view removeFromSuperview];

    }];

}

– (void)minus:(UISwipeGestureRecognizer*)gr

{

    UILabel *l = (UILabel*)gr.view;

    int selected = [l.text intValue];

    int current = [self.label.text intValue];

    self.label.text = [NSString stringWithFormat:@”%04d”, current – selected];

    

    [UIView animateWithDuration:0.6 animations:^{

        gr.view.transform = CGAffineTransformMakeTranslation(-320, 0);

    } completion:^(BOOL finished) {

        [gr.view removeFromSuperview];

    }];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end