iPhone 点と表

点をタップして選んだ形を、表にマッピングしていくiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) UIView *mappingArea;

@property (nonatomic, strong) NSMutableArray *marks;

@property (nonatomic, strong) NSMutableArray *boxArr;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createMappingArea];

    [self createTable];

}

– (void)createMappingArea

{

    UIView *area = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 260, 260)];

    area.backgroundColor = [UIColor lightGrayColor];

    area.layer.cornerRadius = 130;

    [self.view addSubview:area];

    self.mappingArea = area;

    

    self.marks = [NSMutableArray array];

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

        float r = 50 + arc4random() % 50;

        float x = r * cos(i * M_PI/3.0) + 130;

        float y = r * sin(i * M_PI/3.0) + 130;

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

        mark.center = CGPointMake(x, y);

        mark.layer.cornerRadius = 5;

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

        [area addSubview:mark];

        

        [self.marks addObject:mark];

        

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

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

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

        [l sizeToFit];

        l.center = CGPointMake(mark.center.x, mark.center.y  + 18);

        [area addSubview:l];

    }

}

– (void)createTable

{

    self.boxArr = [NSMutableArray array];

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

        float x = (i % 2) * 150 + 15;

        float y = (i / 2) * 60 + 300;

        UIView *box = [[UIView alloc] initWithFrame:CGRectMake(x, y, 140, 50)];

        box.layer.borderWidth = 4;

        box.layer.borderColor = [UIColor grayColor].CGColor;

        [self.view addSubview:box];

        

        [self.boxArr addObject:box];

        

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

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

        l.font = [UIFont boldSystemFontOfSize:30];

        [l sizeToFit];

        l.textColor = [UIColor grayColor];

        l.center = CGPointMake(25, 25);

        [box addSubview:l];

    }

}

– (UIView *)showConsole:(int)idx

{

    UIView *console = [[UIView alloc] initWithFrame:CGRectMake(85, 100, 150, 150)];

    console.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];

    console.layer.cornerRadius = 10;

    console.layer.borderWidth = 3;

    console.layer.borderColor = [UIColor darkGrayColor].CGColor;

    [self.view addSubview:console];

    

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

    title.text = [NSString stringWithFormat:@”%d”, idx + 1];

    title.textColor = [UIColor whiteColor];

    title.center = CGPointMake(10, 10);

    title.font = [UIFont boldSystemFontOfSize:30];

    [title sizeToFit];

    [console addSubview:title];

    

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

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.tag = idx;

        btn.frame = CGRectMake(0, 0, 50, 50);

        btn.backgroundColor = [UIColor grayColor];

        btn.center = CGPointMake(40 + i * 75, 100);

        [btn setTitle:i ? @”○” : @”▲” forState:UIControlStateNormal];

        btn.titleLabel.font = [UIFont fontWithName:@”Avenir-Roman” size:30];

        [console addSubview:btn];

        

        [btn addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];

    }

    

    

    console.transform = CGAffineTransformMakeTranslation(300, 0);

    [UIView animateWithDuration:0.5 animations:^{

        console.transform = CGAffineTransformIdentity;

    }];

    

    return console;

}

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

{

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

    [self.marks enumerateObjectsUsingBlock:^(UIView *v, NSUInteger idx, BOOL *stop) {

        if (CGRectContainsPoint(CGRectMake(v.frame.origin.x10, v.frame.origin.y10, 30, 30), p)) {

            [self showConsole:idx];

        }

    }];

}

– (void)tap:(UIButton *)sender

{

    UIView *console = sender.superview;

    sender.center = [self.view convertPoint:sender.center fromView:console];

    [self.view addSubview:sender];

    

    [UIView animateWithDuration:0.5 animations:^{

        console.transform = CGAffineTransformMakeTranslation(300, 0);

    } completion:^(BOOL finished) {

        [console removeFromSuperview];

    }];

    

    [UIView animateWithDuration:0.5 animations:^{

        sender.center = [self.boxArr[sender.tag] center];

    }];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end