iPhone たしざんガジェット

数字をクルクル合わせて、ボタンを押すとガジェットが開いて数式がでてくる!という感じで足し算のお勉強iPhoneアプリのサンプルコードを描いてみます。


今回使った画像(マスク用)


サンプルを動かすとこんな感じです。

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (weak, nonatomic) UIView *topNumberDisk;

@property (weak, nonatomic) UIView *bottomNumberDisk;

@property CGPoint touchPoint;

@property (weak, nonatomic ) UIView *selected;

@property CGAffineTransform lastTransform;

@end

@implementation ViewController

– (void)viewDidLoad

{

    self.view.backgroundColor = [UIColor greenColor];

    [super viewDidLoad];

    

    [self createDisk];

}

– (void)createDisk

{

    // top disk and bottom disk

    NSArray *maskTypes = @[@”maskA”, @”maskB”];

    for (NSString *maskType in maskTypes) {

        UIView *disk = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];

        disk.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));

        disk.layer.cornerRadius = disk.frame.size.width * 0.5;

        disk.backgroundColor = [UIColor redColor];

        

        [self.view addSubview:disk];

        

        CALayer *mask = [CALayer layer];

        mask.frame = disk.bounds;

        mask.contents = (id)[UIImage imageNamed:maskType].CGImage;

        disk.layer.mask = mask;

        

        [disk addSubview:[self numberDisk]];

        

        if ([maskType isEqual:@”maskA”]) {

            self.topNumberDisk = disk;

        } else {

            self.bottomNumberDisk = disk;

        }

    }

    

    UIButton *plus = [UIButton buttonWithType:UIButtonTypeSystem];

    plus.frame = CGRectMake(0, 0, 80, 80);

    plus.center = CGPointMake(150, 150);

    plus.titleLabel.font = [UIFont boldSystemFontOfSize:80];

    [plus setTitle:@”+” forState:UIControlStateNormal];

    [self.bottomNumberDisk addSubview:plus];

    

    [plus addTarget:self action:@selector(calculate) forControlEvents:UIControlEventTouchUpInside];

    

    CGPoint points[] = {CGPointMake(150, 50), CGPointMake(150, 250)};

    NSArray *arr = @[self.topNumberDisk, self.bottomNumberDisk];

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

        UIView *mark = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];

        mark.center = points[i];

        mark.backgroundColor = [UIColor clearColor];

        mark.layer.borderWidth = 5;

        mark.layer.borderColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.5].CGColor;

        mark.layer.cornerRadius = 10;

        mark.userInteractionEnabled = NO;

        [arr[i] addSubview:mark];

    }

    

}

#define NumberDiskTag 1

– (UIView*)numberDisk

{

    UIView *ndisk = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 280)];

    ndisk.tag = NumberDiskTag;

    ndisk.center = CGPointMake(150, 150);

    ndisk.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];

    ndisk.layer.cornerRadius = 140;

    

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

        UILabel *l = [[UILabel alloc] init];

        l.text = [@(i) stringValue];

        l.font = [UIFont systemFontOfSize:60];

        l.textColor = [UIColor darkGrayColor];

        [l sizeToFit];

        

        float angle = i * (M_PI/4.5);

        float x = 100 * cos(angle) + 140;

        float y = 100 * sin(angle) + 140;

        l.center = CGPointMake(x, y);

        l.transform = CGAffineTransformMakeRotation(angle + M_PI/2.0);

        [ndisk addSubview:l];

        

        l.userInteractionEnabled = YES;

    }

    

    return ndisk;

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInView:self.topNumberDisk];

    self.touchPoint = p;

    

    UIView *ndisk;

    if (p.y < 150)

        ndisk = [self.topNumberDisk viewWithTag:NumberDiskTag];

    else

        ndisk = [self.bottomNumberDisk viewWithTag:NumberDiskTag];

    

    if (CGRectContainsPoint(ndisk.frame, p)) {

        self.selected = ndisk;

        self.lastTransform = ndisk.transform;

    }

}

– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInView:self.topNumberDisk];

    

    float angleA = atan2f(self.touchPoint.x150, self.touchPoint.y150);

    float angleB = atan2f(p.x150, p.y150);

    float deltaAngle = angleA – angleB;

    

    self.selected.transform = CGAffineTransformRotate(self.lastTransform, deltaAngle);

}

– (void)calculate

{

    __block void (^aBlock)(void);

    CGAffineTransform topTransform;

    CGAffineTransform bottomTransform;

    

    if (CGAffineTransformIsIdentity(self.topNumberDisk.transform)) {

        [self showExpression];

        topTransform = CGAffineTransformMakeTranslation(0, –100);

        bottomTransform = CGAffineTransformMakeTranslation(0, 100);

        aBlock = ^void{};

    } else {

        topTransform = CGAffineTransformIdentity;

        bottomTransform = CGAffineTransformIdentity;

        aBlock = ^void{

            UIView *v = [self.view viewWithTag:2];

            [v removeFromSuperview];

        };

    }

    

    [UIView animateWithDuration:1.0 animations:^{

        self.topNumberDisk.transform = topTransform;

        self.bottomNumberDisk.transform = bottomTransform;

    } completion:^(BOOL finished) {

        dispatch_async(dispatch_get_main_queue(), aBlock);

    }];

}

– (void)showExpression

{

    id top = [self.topNumberDisk hitTest:CGPointMake(150, 50) withEvent:nil];

    id bottom = [self.bottomNumberDisk hitTest:CGPointMake(150, 250) withEvent:nil];

    

    if ([top isKindOfClass:[UILabel class]] && [bottom isKindOfClass:[UILabel class]]) {

        int topNum = [[top text] intValue];

        int bottomNum = [[bottom text] intValue];

        int answer = topNum + bottomNum;

        

        UILabel *expression = [[UILabel alloc] init];

        expression.tag = 2;

        expression.text = [NSString stringWithFormat:@”%d + %d = %d”, topNum, bottomNum, answer];

        expression.font = [UIFont systemFontOfSize:60];

        expression.textColor = [UIColor lightGrayColor];

        [expression sizeToFit];

        expression.center = self.topNumberDisk.center;

        [self.view insertSubview:expression atIndex:0];

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end