ファミコンの16連打って昔あったなぁ、とおもいつつサンプル作ってみた。

(XcodeのiOS6 Simulatorで試しています。)

ポイント

・ABボタンを押すと数字をカウントアップ

・十時ボタンは飾りで付けてみた。

ボタンを押した回数をカウントするだけアプリのサンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (nonatomic, strong) UILabel *counter;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    // ファミコンのコントローラーみたいなのを作る

    [self createController];

    

    // 何回叩けたかの表示用

    self.counter = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 310, 200)];

    self.counter.font = [UIFont fontWithName:@”Copperplate” size:50];

    self.counter.textAlignment = 1; // center

    self.counter.text = @”0″;

    [self.view addSubview:self.counter];

}

– (void)createController

{

    // body

    CGSize vSize = self.view.bounds.size;

    float x = 0;

    float y = vSize.height * 0.6;

    float width = vSize.width;

    float height = vSize.height * 0.4;

    UIView *controller = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];

    controller.backgroundColor = [UIColor redColor];

    controller.layer.cornerRadius = 20.0;

    controller.layer.borderWidth = 5.0;

    controller.layer.borderColor = [UIColor blackColor].CGColor;

    [self.view addSubview:controller];

    

    UIView *panel = [[UIView alloc] initWithFrame:CGRectMake(20, 20, width – 180, height – 40)];

    panel.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.6 alpha:1.0];

    panel.layer.cornerRadius = 20.0;

    [controller addSubview:panel];

    

    UIView *panel2 = [[UIView alloc] initWithFrame:CGRectMake(20, 60, width – 40, height – 80)];

    panel2.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.6 alpha:1.0];

    panel2.layer.cornerRadius = 20.0;

    [controller addSubview:panel2];

    

    

    // arrow keys

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

        float x = 40 * sin(M_PI * 0.5 * i) + 70;

        float y = 40 * cos(M_PI * 0.5 * i) + 80;

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

        key.layer.cornerRadius = 5.0;

        key.backgroundColor = [UIColor blackColor];

        [controller addSubview:key];

    }

    

    

    // AB key

    UIView *keyA = [[UIView alloc] initWithFrame:CGRectMake(240, 100, 60, 60)];

    keyA.backgroundColor = [UIColor blackColor];

    keyA.layer.cornerRadius = 30.0;

    keyA.userInteractionEnabled = YES;

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

    [keyA addGestureRecognizer:tgrA];

    [controller addSubview:keyA];

    

    UIView *keyB = [[UIView alloc] initWithFrame:CGRectMake(170, 100, 60, 60)];

    keyB.backgroundColor = [UIColor blackColor];

    keyB.layer.cornerRadius = 30.0;

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

    [keyB addGestureRecognizer:tgrB];

    [controller addSubview:keyB];

    

    

}

– (void)tapA:(UITapGestureRecognizer*)tgr

{

    [self countup];

    [self pushEffect:@”A”];

}

– (void)tapB:(UITapGestureRecognizer*)tgr

{

    [self countup];

    [self pushEffect:@”B”];

}

– (void)countup

{

    int count = [self.counter.text intValue] + 1;

    self.counter.text = [NSString stringWithFormat:@”%d”, count];

}

– (void)pushEffect:(NSString*)type

{

    

    float x = [self.counter.text intValue] % 32 * 10;

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, 500, 0, 0)];

    label.textColor = [UIColor redColor];

    label.text = type;

    label.font = [UIFont boldSystemFontOfSize:50];

    label.backgroundColor = [UIColor clearColor];

    [label sizeToFit];

    [self.view addSubview:label];

    

    [UIView animateWithDuration:0.5 animations:^{

        label.center = CGPointMake(label.center.x, –50);

    } completion:^(BOOL finished) {

        [label removeFromSuperview];

    }];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end