iPhoneすうじを半分に

ポケットをたたくと、ビスケットが二つ。というような、「半分」というのをちょっとだけ体験する簡単なiPhoneアプリを作ってみます。


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

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    int counter;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor whiteColor];

    

    [self createBalls];

}

– (void)createBalls

{

    UIView *ten = [self createBall:10];

    ten.center = CGPointMake(160, 60);

    

    UIView *twenty = [self createBall:20];

    twenty.center = CGPointMake(160, 180);

    

    UIView *thirty = [self createBall:30];

    thirty.center = CGPointMake(160, 350);

}

– (UIView *)createBall:(int)num

{

    UILabel *ball = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 5 * num, 5 * num)];

    ball.backgroundColor = [UIColor redColor];

    ball.layer.cornerRadius = ball.bounds.size.width / 2.0;

    ball.text = [NSString stringWithFormat:@”%d”, num];

    ball.font = [UIFont fontWithName:@”AvenirNext-Heavy” size:num * 2.0];

    ball.textAlignment = NSTextAlignmentCenter;

    ball.textColor = [UIColor whiteColor];

    [self.view addSubview:ball];

    

    ball.userInteractionEnabled = YES;

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

    [ball addGestureRecognizer:tap];

    

    return ball;

}

– (void)splitBall:(UITapGestureRecognizer*)gr

{

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

    CGPoint p1 = CGPointMake(origin.center.x2.0 * origin.bounds.size.width/4.0

                             , origin.frame.origin.y + origin.bounds.size.height/2.0);

    CGPoint p2 = CGPointMake(origin.center.x + 2.0 * origin.bounds.size.width/4.0

                             , origin.frame.origin.y + origin.bounds.size.height/2.0);

    

    UILabel *ball1 = [[UILabel alloc] initWithFrame:origin.frame];

    ball1.backgroundColor = origin.backgroundColor;

    ball1.layer.cornerRadius = origin.layer.cornerRadius;

    ball1.textColor = origin.textColor;

    ball1.textAlignment = origin.textAlignment;

    ball1.text = [NSString stringWithFormat:@”%d”, [origin.text intValue] / 2];

    ball1.font = origin.font;

    [self.view addSubview:ball1];

    UILabel *ball2 = [[UILabel alloc] initWithFrame:origin.frame];

    ball2.backgroundColor = origin.backgroundColor;

    ball2.layer.cornerRadius = origin.layer.cornerRadius;

    ball2.textColor = origin.textColor;

    ball2.textAlignment = origin.textAlignment;

    ball2.text = [NSString stringWithFormat:@”%d”, [origin.text intValue] / 2];

    ball2.font = origin.font;

    [self.view addSubview:ball2];

    

    [UIView animateWithDuration:0.5 animations:^{

        ball1.center = p1;

        ball1.transform = CGAffineTransformMakeScale(1.0 / sqrt(2), 1.0 / sqrt(2));

        

        ball2.center = p2;

        ball2.transform = CGAffineTransformMakeScale(1.0 / sqrt(2), 1.0 / sqrt(2));

    }];

    

    [origin removeFromSuperview];

    

    counter++;

    

    if (counter > 2) {

        // restart

        counter = 0;

        [self performSelector:@selector(restart) withObject:nil afterDelay:1.0];

    }

}

– (void)restart

{

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

        [UIView animateWithDuration:0.5 animations:^{

            v.center = CGPointMake(160, –300);

        } completion:^(BOOL finished) {

            [v removeFromSuperview];

        }];

    }

    [self performSelector:@selector(createBalls) withObject:nil afterDelay:0.6];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end