なぞったりタップしたりでカラフルな波紋を表示してみる

(XcodeのiOS6 iPhone Simulatorで試しています。)

ポイント

・波紋がぶつかったら円の色を変える

・2円の中心と、半径から接触したかどうかを判定

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    CADisplayLink *timer;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    [self createRainbowColor];

    [self start];

}

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

{

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

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

    ripple.backgroundColor = [UIColor clearColor];

    ripple.layer.borderWidth = 3.0;

    ripple.layer.cornerRadius = 5;

    ripple.center = p;

    [self.view addSubview:ripple];

}

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

{

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

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

    ripple.backgroundColor = [UIColor clearColor];

    ripple.layer.borderWidth = 3.0;

    ripple.layer.cornerRadius = 5;

    ripple.center = p;

    [self.view addSubview:ripple];

}

– (void)start

{

    timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateDisp:)];

    [timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

}

//

//Red: (255, 0, 0)

//Orange: (255, 127, 0)

//Yellow: (255, 255, 0)

//Green: (0, 255, 0)

//Blue: (0, 0, 255)

//Indigo: (75, 0, 130)

//Violet: (143, 0, 255)

static NSArray *rainbow;

– (void)createRainbowColor

{

    UIColor *red = [UIColor colorWithRed:255.0/255.0 green:0 blue:0 alpha:1.0];

    UIColor *orange = [UIColor colorWithRed:255.0/255.0 green:127.0/255.0 blue:0 alpha:1.0];

    UIColor *yellow = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:0 alpha:1.0];

    UIColor *green = [UIColor colorWithRed:0 green:255.0/255.0 blue:0 alpha:1.0];

    UIColor *blue = [UIColor colorWithRed:0 green:0 blue:255.0/255.0 alpha:1.0];

    UIColor *indigo = [UIColor colorWithRed:75.0/255.0 green:0 blue:130/255.0 alpha:1.0];

    UIColor *violet = [UIColor colorWithRed:143.0/255.0 green:0 blue:255.0/255.0 alpha:1.0];

    rainbow = [NSArray arrayWithObjects:red, orange, yellow, green, blue, indigo, violet, nil];

}

– (void)updateDisp:(CADisplayLink*)sender

{

    for (UIView *v in self.view.subviews) {

        v.bounds = CGRectMake(0,0,v.bounds.size.width +2, v.bounds.size.height +2);

        v.layer.cornerRadius = v.bounds.size.width * 0.5;

        

        // でかくなったら消す

        if (v.bounds.size.width > 300) {

                [v removeFromSuperview];

        }

    }

    

    // 輪の干渉をチェック

    // 中心と半径で

    for (UIView *v1 in self.view.subviews) {

        for (UIView *v2 in self.view.subviews) {

            if (v1 == v2) continue;

            

            // 中心間の距離

            float d = hypotf(v2.center.x – v1.center.x, v2.center.y – v1.center.y);

            float r = v1.bounds.size.width * 0.5 + v2.bounds.size.width * 0.5;

            if (abs(d – r) < 0.02) {

                v1.tag++;

            }

        }

        if (v1.tag > 0) {

            v1.layer.borderColor = ((UIColor*)[rainbow objectAtIndex:v1.tag % 7]).CGColor;

        }

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end