iPhone足し算リング

リングをクルクルまわして、マーカーのところにきた数字を足していくというシンプルな足し算iPhoneアプリを描いていきます。


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

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

{

    UIView *circleA;

    UILabel *total;

    UIView *mark;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];

    [self createCircleA];

    [self createTotal];

    [self createMark];

}

– (void)createCircleA

{    

    circleA = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 260, 260)];

    circleA.backgroundColor = [UIColor blackColor];

    circleA.layer.borderWidth = 5;

    circleA.layer.borderColor = CGColorCreateCopyWithAlpha([UIColor whiteColor].CGColor, 0.3);

    circleA.layer.cornerRadius = 130;

    circleA.center = CGPointMake(160, 200);

    

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

        float x = 110.0 * cos(M_PI/5.0 * i) + 130;

        float y = 110.0 * sin(M_PI/5.0 * i) + 130;

        

        UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

        l.center = CGPointMake(x, y);

        l.font = [UIFont fontWithName:@”Avenir-Light” size:20];

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

        l.textAlignment = NSTextAlignmentCenter;

        l.backgroundColor = [UIColor clearColor];

        l.textColor = [UIColor colorWithWhite:1 alpha:0.8];

        [circleA addSubview:l];

    }

    [self.view addSubview:circleA];

    

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

    [circleA addGestureRecognizer:pan];

}

– (void)createTotal

{

    total = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];

    total.center = circleA.center;

    total.backgroundColor = [UIColor clearColor];

    total.font = [UIFont fontWithName:@”Avenir-Light” size:50];

    total.text = @”0″;

    total.textAlignment = NSTextAlignmentCenter;

    total.textColor = [UIColor colorWithWhite:1 alpha:0.7];

    [self.view addSubview:total];

}

– (void)createMark

{

    mark = [[UIView alloc] initWithFrame:CGRectMake(10, 180, 80, 40)];

    mark.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];

    mark.layer.cornerRadius = 20;

    [self.view addSubview:mark];

}

– (void)turn:(UIPanGestureRecognizer*)gr

{

    static CGPoint start;

    static CGAffineTransform t;

    if (gr.state == UIGestureRecognizerStateBegan) {

        start = [gr locationInView:self.view];

        t = gr.view.transform;

    }

    

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

    float angle0 = atan2(start.xcircleA.center.x, start.ycircleA.center.y);

    float angle1 = atan2(p.xcircleA.center.x, p.ycircleA.center.y);

    gr.view.transform = CGAffineTransformRotate(t, angle0 – angle1);

    

    float angle = [[gr.view.layer valueForKeyPath:@”transform.rotation”] floatValue];

    for (UIView *l in gr.view.subviews) {

        l.transform = CGAffineTransformMakeRotation(-angle);

    }

    

    if (gr.state == UIGestureRecognizerStateEnded) {

        for (UILabel *l in gr.view.subviews) {

            CGPoint point = [circleA convertPoint:l.center toView:self.view];

            if (CGRectContainsPoint(mark.frame, point)) {

                [self add:[l.text intValue]];

            }

        }

    }

}

– (void)add:(int)number

{

    int totalNumber = [total.text intValue] + number;

    total.text = [NSString stringWithFormat:@”%d”, totalNumber];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end