iPhoneクリスマス電飾

クリスマスツリーにイルミネーションライトをポチポチしてきれいに飾っていけたらいいな。という感じのiPhoneアプリのサンプルコード


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

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) UIButton *selected;

@property (nonatomic, strong) NSMutableArray *lights;

@end

@implementation ViewController

– (void)viewDidLoad

{

    self.view.backgroundColor = [UIColor blackColor];

    [super viewDidLoad];

    [self createTree];

    [self createColorButton];

}

– (void)createTree

{

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

        float x = i * 10;

        float y = i * 20 + 20;

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

            [self dotAtPoint:CGPointMake(160 + ((j==0) ? x : -x) , y) color:[UIColor greenColor]];

        }

    }

    

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

        float y = 350;

        if (i == 6 || i == 7 || i == 8) {

            y = 370;

        }

        [self dotAtPoint:CGPointMake(i * 20 + 20, y) color:[UIColor greenColor]];

    }

}

– (void)dotAtPoint:(CGPoint)p color:(UIColor*)color

{

    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(p.x, p.y, 10, 10)];

    greenView.backgroundColor = [UIColor greenColor];

    [self.view addSubview:greenView];

}

– (void)createColorButton

{

    NSArray *colors = @[

            [UIColor redColor],

            [UIColor yellowColor],

            [UIColor blueColor],

            [UIColor orangeColor],

            ];

    

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

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

        btn.frame = CGRectMake(i * 60 + 55, 420, 40, 40);

        btn.backgroundColor = colors[i];

        [self.view addSubview:btn];

        

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

    }

}

– (void)touch:(UIButton*)sender

{

    self.selected = sender;

    sender.backgroundColor = [sender.backgroundColor colorWithAlphaComponent:1.0];

    sender.layer.shadowColor = sender.backgroundColor.CGColor;

    sender.layer.shadowOpacity = 0.8;

    sender.layer.shadowOffset = CGSizeMake(0, 0);

    sender.layer.shadowRadius = 10;

    

    NSPredicate *pre = [NSPredicate predicateWithFormat: @”class == %@”, [UIButton class]];

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

        if (obj != sender) {

            [obj setBackgroundColor:[[obj backgroundColor] colorWithAlphaComponent:0.3]];

            [obj layer].shadowOpacity = 0;

        }

    }];

}

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

{

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

    

    if (!self.lights) {

        self.lights = [[NSMutableArray alloc] init];

    }

    

    if (self.selected) {

        UIView *light = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

        light.layer.cornerRadius = 10;

        light.backgroundColor = self.selected.backgroundColor;

        light.center = p;

        [self.view addSubview:light];

        

        light.layer.shadowColor = light.backgroundColor.CGColor;

        light.layer.shadowOpacity = 0.8;

        light.layer.shadowOffset = CGSizeMake(0, 0);

        light.layer.shadowRadius = 10;

        

        CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@”opacity”];

        fade.duration = 0.8f;

        fade.repeatCount = HUGE_VAL;

        fade.removedOnCompletion = NO;

        fade.fillMode = kCAFillModeForwards;

        fade.toValue = @0.1;

        [light.layer addAnimation:fade forKey:nil];

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end