iPhone 六角和算

六角形の数字を足して真ん中が答えになるようにするiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController () <UITextFieldDelegate>

@property (nonatomic, strong) NSMutableArray *numbers;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    [self createHexSheet];

    [self drawLines];

    [self createLabels];

}

– (void)createHexSheet

{

    CGPoint o = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));

    

    UIView *hex = [self createHexWithRadius:160];

    hex.center = o;

    

    UIView *hexMid = [self createHexWithRadius:90];

    hexMid.center = o;

    

    UIView *hexCenter = [self createHexWithRadius:30];

    hexCenter.center = o;

}

– (UIView *)createHexWithRadius:(float)r

{

    UIView *hex = [[UIView alloc] init];

    [self.view addSubview:hex];

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    float d = M_PI / 3.0;

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

        float x = r * cos(d * i);

        float y = r * sin(d * i);

        if (i==0)

            [path moveToPoint:CGPointMake(x, y)];

        else

            [path addLineToPoint:CGPointMake(x, y)];

    }

    [path closePath];

    CAShapeLayer *l = [CAShapeLayer layer];

    l.path = path.CGPath;

    l.strokeColor = [UIColor lightGrayColor].CGColor;

    l.fillColor = [[UIColor greenColor] colorWithAlphaComponent:0.3].CGColor;

    l.lineWidth = 2;

    [hex.layer addSublayer:l];

    

    return hex;

}

– (void)drawLines

{

    CGPoint o = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));

    UIBezierPath *path = [UIBezierPath bezierPath];

    float d = M_PI / 3.0;

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

        float x0 = 30 * cos(d * i) + o.x;

        float y0 = 30 * sin(d * i) + o.y;

        float x1 = 160 * cos(d * i) + o.x;

        float y1 = 160 * sin(d * i) + o.y;

        [path moveToPoint:CGPointMake(x0, y0)];

        [path addLineToPoint:CGPointMake(x1, y1)];

    }

    

    CAShapeLayer *l = [CAShapeLayer layer];

    l.path = path.CGPath;

    l.strokeColor = [UIColor lightGrayColor].CGColor;

    l.fillColor = [UIColor clearColor].CGColor;

    l.lineWidth = 2;

    

    [self.view.layer addSublayer:l];

}

– (void)createLabels

{

    self.numbers = [NSMutableArray array];

    

    CGPoint o = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame));

    UITextField *f = [self mytextField];

    f.center = o;

    f.text = [@((arc4random() % 3) + 7) stringValue];

    [self.view addSubview:f];

    [self.numbers addObject:f];

    

    float d = M_PI / 3.0;

    NSMutableArray *seed = [@[@”1″, @”2″, @”3″, @”4″, @”5″, @”6″] mutableCopy];

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

        float angle = d * i + M_PI / 6.0;

        

        UITextField *f2 = [self mytextField];

        f2.center = CGPointMake(55 * cos(angle) + o.x, 55 * sin(angle) + o.y);

        f2.userInteractionEnabled = YES;

        [self.view addSubview:f2];

        

        UITextField *f3 = [self mytextField];

        f3.center = CGPointMake(110 * cos(angle) + o.x, 110 * sin(angle) + o.y);

        int idx = arc4random() % seed.count;

        f3.text = seed[idx];

        [seed removeObjectAtIndex:idx];

        [self.view addSubview:f3];

        

        [self.numbers addObject:f2];

        [self.numbers addObject:f3];

    }

}

– (UITextField *)mytextField

{

    UITextField *f = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    f.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.1];

    f.layer.cornerRadius = 20;

    f.font = [UIFont boldSystemFontOfSize:30];

    f.textColor = [UIColor whiteColor];

    f.textAlignment = NSTextAlignmentCenter;

    f.userInteractionEnabled = NO;

    f.keyboardType = UIKeyboardTypeNumberPad;

    f.delegate = self;

    return f;

}

– (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

    textField.text = [string substringToIndex:0];

    

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        [self check];

    });

    

    return YES;

}

– (void)check

{

    [self.view endEditing:YES];

    

    BOOL clear = YES;

    NSUInteger ans = [[self.numbers[0] text] intValue];

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

        NSUInteger add = [[self.numbers[2 * i + 1] text] intValue] + [[self.numbers[2 * i + 2] text] intValue];

        if (ans != add) {

            clear = NO;

        }

    }

    

    if (clear) {

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

        clearLabel.text = @”clear”;

        clearLabel.font = [UIFont boldSystemFontOfSize:70];

        [clearLabel sizeToFit];

        clearLabel.center = CGPointMake(CGRectGetMidX(self.view.frame), 160);

        [self.view addSubview:clearLabel];

        

        clearLabel.transform = CGAffineTransformMakeScale(0, 0);

        [UIView animateWithDuration:0.5 animations:^{

            clearLabel.transform = CGAffineTransformIdentity;

        }];

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end