iPhone 青海波模様

波がぴょこっとランダムに跳ねるような青海波模様を描いてみます。


動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

{

    NSMutableArray *wave;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createBlueWave];

    

    [self start];

}

– (void)createBlueWave

{

    int rows = 11;

    int cols = 60;

    float w = 350.0 / rows;

    float h = 600.0 / cols;

    for (int i=0; i<rows*cols; i++) {

        float x = (i % rows) * w + ((i/rows % 2) ? w/2.0 : 0);

        float y = (i / rows) * h – h/2.0;

        [self blueCircle:CGPointMake(x, y)];

    }

}

– (void)blueCircle:(CGPoint)p

{

    float r = 34;

    UIView *b = [[UIView alloc] initWithFrame:CGRectMake(0, 0, r, r)];

    b.backgroundColor = [UIColor blueColor];

    b.layer.cornerRadius = r/2.0;

    b.center = p;

    [self.view addSubview:b];

    

    UIView *b1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, r * 0.85, r * 0.85)];

    b1.layer.borderColor = [UIColor whiteColor].CGColor;

    b1.layer.borderWidth = 2;

    b1.layer.cornerRadius = (r * 0.85)/2.0;

    b1.center = CGPointMake(r/2.0, r/2.0);

    [b addSubview:b1];

    

    UIView *b2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, r * 0.6, r * 0.6)];

    b2.layer.borderColor = [UIColor whiteColor].CGColor;

    b2.layer.borderWidth = 2;

    b2.layer.cornerRadius = (r * 0.6)/2.0;

    b2.center = CGPointMake(r/2.0, r/2.0);

    [b addSubview:b2];

    

    UIView *b3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, r * 0.3, r * 0.3)];

    b3.backgroundColor = [UIColor whiteColor];

    b3.layer.cornerRadius = (r * 0.3)/2.0;

    b3.center = CGPointMake(r/2.0, r/2.0);

    [b addSubview:b3];

    

    if (!wave) {

        wave = [[NSMutableArray alloc] init];

    }

    [wave addObject:b];

}

– (void)start

{

    [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(updown) userInfo:nil repeats:YES];

}

– (void)updown

{

    int n = arc4random() % [wave count];

    UIView *b = [wave objectAtIndex:n];

    

    [UIView animateWithDuration:0.3 animations:^{

        b.transform = CGAffineTransformMakeTranslation(0, –50);

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.3 animations:^{

            b.transform = CGAffineTransformIdentity;

        }];

    }];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end