iPhone クジラ

クジラをタッチすると、背中から潮を噴き出します!という単純なiPhoneアプリを作っていきます。


動作イメージ

XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。

ポイント
UILongPressGestureRecognizerを使って、タッチでタイマーをスタートして、指を離してから、1.5秒後にタイマーを停止するようにしました。遅延はperformSelectorを使っています。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface WaterDrop : UIView

@property (nonatomic, assign) CGPoint velocity;

@end

@implementation WaterDrop

@synthesize velocity;

@end

@interface ViewController ()

{

    UIView *seaFront;

    UIView *whale;

    NSTimer *timer;

    float tcounter;

    BOOL stop;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [self color:0];

    

    [self createSea];

    

    [self createWhale];

}

– (void)createSea

{

    UIView *sea = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 320, 256)];

    sea.backgroundColor = [self color:2];

    [self.view addSubview:sea];

    

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

    [path moveToPoint:CGPointZero];

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

        [path addQuadCurveToPoint:CGPointMake(i * 60 + 60, 0) controlPoint:CGPointMake(i*60 + 30, 30)];

    }

    [path addLineToPoint:CGPointMake(320, 500)];

    [path addLineToPoint:CGPointMake(0, 500)];

    [path closePath];

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = sea.backgroundColor.CGColor;

    sl.path = path.CGPath;

    

    seaFront = [[UIView alloc] initWithFrame:CGRectMake(0, 400, 320, 256)];

    [self.view addSubview:seaFront];

    [seaFront.layer addSublayer:sl];

    

    CABasicAnimation *wave = [CABasicAnimation animationWithKeyPath:@”transform.translation.x”];

    wave.autoreverses = YES;

    wave.duration = 2.0;

    wave.toValue = @50;

    wave.repeatCount = HUGE_VAL;

    

    [sl addAnimation:wave forKey:nil];

    

}

– (void)createWhale

{

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

    [path moveToPoint:CGPointMake(0, 130)];

    [path addCurveToPoint:CGPointMake(150, 130) controlPoint1:CGPointMake(0, 10) controlPoint2:CGPointMake(150, 10)];

    [path closePath];

    [path moveToPoint:CGPointMake(130, 130)];

    [path addLineToPoint:CGPointMake(180, 80)];

    [path addLineToPoint:CGPointMake(180, 130)];

    [path closePath];

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = [self color:1].CGColor;

    sl.path = path.CGPath;

    

    whale = [[UIView alloc] initWithFrame:CGRectMake(70, 300, 180, 100)];

    [self.view insertSubview:whale belowSubview:seaFront];

    [whale.layer addSublayer:sl];

    

    UIView *eye = [[UIView alloc] initWithFrame:CGRectMake(30, 90, 5, 5)];

    eye.backgroundColor = [self color:0];

    eye.layer.cornerRadius = 2.5;

    [whale addSubview:eye];

    

    UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(press:)];

    press.minimumPressDuration = 0;

    [whale addGestureRecognizer:press];

}

– (void)press:(UILongPressGestureRecognizer*)gr

{

    if (gr.state == UIGestureRecognizerStateBegan) {

        stop = NO;

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

    } else {

        stop = YES;

        [timer performSelector:@selector(invalidate) withObject:nil afterDelay:2.0];

    }

}

– (void)spouts:(NSTimer*)sender

{

    tcounter += sender.timeInterval;

    if (tcounter > 0.1 && !stop) {

        WaterDrop *wdrop = [[WaterDrop alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];

        [self.view addSubview:wdrop];

        wdrop.backgroundColor = [self color:1];

        wdrop.center = CGPointMake(140, 330);

        wdrop.layer.cornerRadius = 5.0;

        int seed = (1arc4random() % 3);

        wdrop.velocity = CGPointMake(2 * seed, –20);

        [self.view addSubview:wdrop];

        tcounter = 0;

    }

    

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

        if ([v isKindOfClass:[WaterDrop class]]) {

            WaterDrop *wd = (WaterDrop*)v;

            wd.velocity = CGPointMake(wd.velocity.x, wd.velocity.y + 1);

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

            if (wd.center.y > 568) {

                [wd removeFromSuperview];

            }

        }

    }

}

#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

– (UIColor*)color:(int)i

{

    switch (i) {

        case 0:

            return UIColorHex(0xFAFAFA);

        case 1:

            return UIColorHex(0x026BB5);

        case 2:

            return UIColorHex(0x00AAE3);

        case 3:

            return UIColorHex(0xFF7729);

        case 4:

            return UIColorHex(0x474747);

        default:

            break;

    }

    return nil;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end