iPhoneコネクト

線で繋いで丸を大きく明るくしていくiPhoneアプリのサンプルコードを描いてみます。

— movie —

— sample code—

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) UIView *selected;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];

    [self createTitle];

    [self createNodes];

}

– (void)createTitle

{

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

    title.text = @”con~nect”;

    title.font = [UIFont boldSystemFontOfSize:30];

    title.textColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6];

    [title sizeToFit];

    title.center = CGPointMake(160, 50);

    [self.view addSubview:title];

    

    title.userInteractionEnabled = YES;

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

    [title addGestureRecognizer:tap];

}

– (void)createNodes

{

    float R = 30;

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

        if (arc4random() % 3) {

            continue;

        }

        float x = (i % 4) * 80 + 40;

        float y = (i / 4) * 60 + 120;

        UIView *node = [[UIView alloc] initWithFrame:CGRectMake(0, 0, R, R)];

        node.tag = 1;

        node.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.3];

        node.center = CGPointMake(x, y);

        node.layer.cornerRadius = R / 2.0;

        node.layer.borderColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6].CGColor;

        

        [self.view addSubview:node];

    }

}

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

{

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

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

        if (v.tag == 1 && CGRectContainsPoint(v.frame, p)) {

            if (self.selected) {

                [self connect:v];

                self.selected.layer.borderWidth = 0;

                self.selected = nil;

            } else {

                self.selected = v;

                self.selected.layer.borderWidth = 4;

            }

        }

    }];

}

– (void)connect:(UIView*)v

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:self.selected.center];

    [path addLineToPoint:v.center];

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.path = path.CGPath;

    sl.strokeColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5].CGColor;

    sl.lineWidth = 4;

    [self.view.layer addSublayer:sl];

    

    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@”strokeEnd”];

    pathAnimation.duration = 0.5;

    pathAnimation.fromValue = @(0.0f);

    pathAnimation.toValue = @(1.0f);

    [sl addAnimation:pathAnimation forKey:@”strokeEnd”];

    

    NSArray *views = @[self.selected, v];

    for (UIView *v in views) {

        v.transform = CGAffineTransformScale(v.transform, 1.1, 1.1);

        const CGFloat* components = CGColorGetComponents(v.backgroundColor.CGColor);

        v.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:components[3] + 0.1];

    }

}

– (void)reset

{

    [self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        [obj removeFromSuperview];

    }];

    self.view.layer.sublayers = nil;

    

    [self createTitle];

    [self createNodes];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end