タッチすると、さかながどんどん増えていくiPhoneゲームサンプル
魚の色は、ランダムに赤、緑、オレンジ、グレー。
増えれば増えるほど、速い魚が生まれてくるので、
たくさんタッチすると、なんとも言えない絵になります。

ポイント
CAShapeLayerの中に、魚の口から尾びれまでのカーブを
addCurveToPointを使ってパスをつくり、これを
上下二本書いてやることで、魚を描画しています。



サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    NSTimer *timer;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blueColor];

    

    [self start];

}

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

{

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

    [self createFish:p];

}

– (void)createFish:(CGPoint)p

{

    NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor orangeColor], [UIColor greenColor], [UIColor purpleColor], [UIColor grayColor], nil];

    

    float w = 40;

    float h = 30;

    

    UIView *fish = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h)];

    fish.backgroundColor = [UIColor clearColor];

    fish.center = p;

    [self.view addSubview:fish];

    

    UIBezierPath *path = [[UIBezierPath alloc] init];

    CGPoint mouth = CGPointMake(0, h * 0.5);

    CGPoint tail1 = CGPointMake(w, h * 0.8);

    CGPoint tail2 = CGPointMake(w, h * 0.2);

    CGPoint cp1 = CGPointMake(0, 0);

    CGPoint cp2 = CGPointMake(w, h * 0.5);

    [path moveToPoint:mouth];

    [path addCurveToPoint:tail1 controlPoint1:cp1 controlPoint2:cp2];

    [path addLineToPoint:tail2];

    

    cp1 = CGPointMake(w, h * 0.5);

    cp2 = CGPointMake(0, h);

    [path addCurveToPoint:mouth controlPoint1:cp1 controlPoint2:cp2];

    

    CAShapeLayer *l = [[CAShapeLayer alloc] initWithLayer:fish.layer];

    l.strokeColor = [UIColor whiteColor].CGColor;

    l.lineWidth = 2.0;

    UIColor *color = [colors objectAtIndex:arc4random() % [colors count]];

    l.fillColor = color.CGColor;

    l.path = path.CGPath;

    

    [fish.layer addSublayer:l];

    

    // eye

    CALayer *eye = [[CALayer alloc] init];

    eye.frame = CGRectMake(w * 0.15,h * 0.4, 4, 4);

    eye.cornerRadius = 2;

    eye.backgroundColor = [UIColor whiteColor].CGColor;

    

    [fish.layer addSublayer:eye];

    

    fish.tag = 1;

}

– (void)start

{

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];

}

– (void)tick:(NSTimer*)sender

{

    // new fish is fast!

    CGPoint velocity;

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

        if (v.tag == 0) {

            continue;

        }

        

        [self checkBoundary:v];

                

        if (CGAffineTransformEqualToTransform(v.transform, CGAffineTransformIdentity)) {

            velocity.x -= 1;

            velocity.y =0;

        } else {

            velocity.x += 1;

            velocity.y =0;

        }

        v.center = CGPointMake(v.center.x + velocity.x, v.center.y + velocity.y);

    }

}

– (void)checkBoundary:(UIView*)v

{

    BOOL transformed = !CGAffineTransformEqualToTransform(v.transform, CGAffineTransformIdentity);

    

    if (v.center.x < 50 && !transformed) {

        [UIView animateWithDuration:0.5 animations:^{

            v.transform = CGAffineTransformMakeRotation(M_PI);

        }];

    } else if (v.center.x > 270 && transformed){

        [UIView animateWithDuration:0.5 animations:^{

            v.transform = CGAffineTransformIdentity;

        }];

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end