第6回「時計 お昼12:00」です。

(※東京リトルロケット(Tokyo Little Rocket)は

4コマ漫画をプログラムで動かそうという企画です)

今週のあらすじ

主人公・豹柄ウサギのヒョードル(豹柄は見えたり見えなかったり)マイペースなのんびりやさん。
精霊針・ノッポさん(リーダー)、チビさん(あわてん坊)、おデブさん(カレーが好物)。合体して時計の針になったり、ならなかったり。
遊びに夢中のヒョードルは
お昼になっても帰りません。そこで精霊針達は集まって何やら相談してる様子。

四コマの絵


サンプルコード

(Xcode iOS6 iPhone Simulatorで動かしています。)

@interface OutLineLabel : UILabel

@end

@implementation OutLineLabel

– (void)drawTextInRect:(CGRect)rect

{

    CGSize shadowOffset = self.shadowOffset;

    UIColor *textColor = self.textColor;

    

    CGContextRef c = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(c, 10);

    CGContextSetLineJoin(c, kCGLineJoinRound);

    

    CGContextSetTextDrawingMode(c, kCGTextStroke);

    self.textColor = [UIColor whiteColor];

    [super drawTextInRect:rect];

    

    CGContextSetTextDrawingMode(c, kCGTextFill);

    self.textColor = textColor;

    self.shadowOffset = CGSizeMake(0, 0);

    [super drawTextInRect:rect];

    

    self.shadowOffset = shadowOffset;

}

@end

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

#import <AVFoundation/AVFoundation.h>

@interface ViewController () {

    UIView *startView;

    

    UIImage *backSceneImg;

    UIImage *backScene2Img;

    UIImage *clockImg;

    UIImage *ballImg;

    UIImage *rabitsweatImg, *rabitface1Img, *rabitface2Img;

    UIImage *rabitmarkImg, *rabitfootImg, *rabitbodyImg;

    UIImage *hourhandImg, *minutehandImg, *secondhandImg;

    UIImage *comment1, *comment2, *comment3, *comment4;

    UIImage *gyurugyuruImg;

    

    UIImageView *backScene;

    UIImageView *clock;

    UIImageView *ball;

    UIImageView *rabitsweat;

    UIImageView *rabitface;

    UIImageView *rabitmark;

    UIImageView *rabitfoot;

    UIImageView *rabitbody;

    UIImageView *hourhand, *minutehand, *secondhand;

    UIImageView *comment;

    UIImageView *gyurugyuru;

    

    AVAudioPlayer *player;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    [self prepareSound];

    

    self.view.backgroundColor = [UIColor blackColor];

    [self loadResource];

    [self createOpening];

}

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

{

    static int lock;

    if(lock == 0) {

        lock++;

        [self showProgram];

    }

    

    CGPoint p = [[touches anyObject] locationInView:rabitfoot];

    NSLog(@”%f %f”, p.x, p.y);

}

– (void)loadResource

{

    backSceneImg = [UIImage imageNamed:@”backScene”];

    backScene2Img = [UIImage imageNamed:@”backScene2″];

    clockImg = [UIImage imageNamed:@”clock”];

    ballImg = [UIImage imageNamed:@”ball”];

    rabitsweatImg = [UIImage imageNamed:@”rabitsweat”];

    rabitface1Img = [UIImage imageNamed:@”rabitface1″];

    rabitface2Img = [UIImage imageNamed:@”rabitface2″];

    rabitmarkImg = [UIImage imageNamed:@”rabitmark”];

    rabitfootImg = [UIImage imageNamed:@”rabitfoot”];

    rabitbodyImg = [UIImage imageNamed:@”rabitbody”];

    hourhandImg = [UIImage imageNamed:@”hourhand”];

    minutehandImg = [UIImage imageNamed:@”minutehand”];

    secondhandImg = [UIImage imageNamed:@”secondhand”];

    comment1 = [UIImage imageNamed:@”comment1″];

    comment2 = [UIImage imageNamed:@”comment2″];

    comment3 = [UIImage imageNamed:@”comment3″];

    comment4 = [UIImage imageNamed:@”comment4″];

    gyurugyuruImg = [UIImage imageNamed:@”gyurugyuru”];

}

– (void)showProgram

{

    // 幕を上げる

    [UIView animateWithDuration:1.0 animations:^{

        startView.center = CGPointMake(250, –300);

    } completion:^(BOOL finished) {

        [startView removeFromSuperview];

    }];

    

    // シーン 1

    float delay = 0;

    [self showScene:^{

        [self scene1];

    } afterDelay:delay];

    

    // シーン 2

    delay += 10;

    [self showScene:^{

        [self scene2];

    } afterDelay:delay];

    

    // シーン 3

    delay += 6;

    [self showScene:^{

        [self scene3];

    } afterDelay:delay];

    

    // シーン 4

    delay += 5;

    [self showScene:^{

        [self scene4];

    } afterDelay:delay];

    

    // end

    delay += 5;

    [self showScene:^{

        [self createEnding];

    } afterDelay:delay];

    

}

– (void)scene1

{

    

    backScene = [[UIImageView alloc] initWithImage:backSceneImg];

    backScene.frame = self.view.bounds;

    [self.view insertSubview:backScene belowSubview:startView];

    

    rabitbody = [[UIImageView alloc] initWithImage:rabitbodyImg];

    rabitbody.frame = self.view.bounds;

    rabitbody.transform = CGAffineTransformMakeScale(0.5, 0.5);

    rabitbody.center = CGPointMake(rabitbody.center.x, rabitbody.center.y + 50);

    [backScene addSubview:rabitbody];

    

    rabitface = [[UIImageView alloc] initWithImage:rabitface1Img];

    rabitface.frame = self.view.bounds;

    [rabitbody addSubview:rabitface];

    

    rabitfoot = [[UIImageView alloc] initWithImage:rabitfootImg];

    rabitfoot.frame = self.view.bounds;

    [rabitbody addSubview:rabitfoot];

    rabitfoot.layer.anchorPoint = CGPointMake(200.0/self.view.bounds.size.width, 210.0/self.view.bounds.size.height);

    rabitfoot.center = CGPointMake(200, 210);

    

    ball = [[UIImageView alloc] initWithImage:ballImg];

    ball.frame = self.view.bounds;

    [backScene addSubview:ball];

    ball.transform = CGAffineTransformMakeScale(0.5, 0.5);

    ball.center = CGPointMake(130,200);

    

    

    // リフティング

    float delay = 0.5;

    [UIView animateWithDuration:5.0 animations:^{

        rabitbody.center = CGPointMake(rabitbody.center.x60, rabitbody.center.y);

    }];

    

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

        [self showScene:^{

            [UIView animateWithDuration:0.1 animations:^{

                rabitfoot.transform = CGAffineTransformMakeRotation(0.4 * M_PI);

            } completion:^(BOOL finished) {

                

                // キック音

                [self playSound:@”kick”];

                

                [UIView animateWithDuration:0.2 animations:^{

                    rabitfoot.transform = CGAffineTransformIdentity;

                }];

            }];

            [UIView animateWithDuration:0.5 animations:^{

                ball.center = CGPointMake(ball.center.x7, ball.center.y100);

            } completion:^(BOOL finished) {

                [UIView animateWithDuration:0.5 animations:^{

                    ball.center = CGPointMake(ball.center.x7, ball.center.y + 100);

                }];

            }];

        } afterDelay:delay];

        delay += 1.0;

    }

    

    [self showScene:^{

        //コメント「お昼よ~」

        comment = [[UIImageView alloc] initWithImage:comment1];

        comment.frame = self.view.bounds;

        [self.view addSubview:comment];

        [UIView animateWithDuration:2.0 animations:^{

            comment.alpha = 0;

            comment.transform = CGAffineTransformMakeTranslation(300, –100);

        } completion:^(BOOL finished) {

            comment.transform = CGAffineTransformIdentity;

        }];

    } afterDelay:delay];

    

    // ヒョードル反対方向へ歩いていく

    delay += 0.5;

    [self showScene:^{

        [UIView animateWithDuration:0.5 animations:^{

            rabitbody.transform = CGAffineTransformScale(rabitbody.transform, –1.0, 1.0);

            ball.center = CGPointMake(ball.center.x + 100, ball.center.y);

        }];

    } afterDelay:delay];

    

    

    // ヒョードル:「まだ遊びたーい」

    delay += 2.0;

    [self showScene:^{

        comment.image = comment2;

        comment.alpha = 1.0;

        [UIView animateWithDuration:3.0 animations:^{

            comment.alpha = 0;

            comment.transform = CGAffineTransformMakeTranslation(100, 100);

        } completion:^(BOOL finished) {

            comment.transform = CGAffineTransformIdentity;

        }];

    } afterDelay:delay];

    

    

    // またリフティング

    delay -= 0.5;

    [self showScene:^{

        [UIView animateWithDuration:5.0 animations:^{

            rabitbody.center = CGPointMake(rabitbody.center.x +50, rabitbody.center.y);

        }];

    } afterDelay:delay];

    

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

        [self showScene:^{

            [UIView animateWithDuration:0.1 animations:^{

                rabitfoot.transform = CGAffineTransformMakeRotation(0.4 * M_PI);

            } completion:^(BOOL finished) {

                

                // キック音

                if (i<4) {

                    [self playSound:@”kick”];

                }

                

                [UIView animateWithDuration:0.2 animations:^{

                    rabitfoot.transform = CGAffineTransformIdentity;

                }];

            }];

            [UIView animateWithDuration:0.5 animations:^{

                ball.center = CGPointMake(ball.center.x + 5, ball.center.y100);

            } completion:^(BOOL finished) {

                [UIView animateWithDuration:0.5 animations:^{

                    ball.center = CGPointMake(ball.center.x + 5, ball.center.y + 100);

                }];

            }];

        } afterDelay:delay];

        delay += 1.0;

    }

    

}

– (void)scene2

{

    // 背景変更

    [UIView animateWithDuration:0.5 animations:^{

        backScene.alpha = 0;

        rabitbody.alpha = 0;

        ball.alpha = 0;

    } completion:^(BOOL finished) {

        backScene.image = backScene2Img;

        

        // 時計

        clock = [[UIImageView alloc] initWithImage:clockImg];

        clock.frame = self.view.bounds;

        [backScene addSubview:clock];

        clock.transform = CGAffineTransformMakeScale(2.0, 2.0);

        clock.center = CGPointMake(self.view.center.x, 0);

        

        // 長針

        minutehand = [[UIImageView alloc] initWithImage:minutehandImg];

        minutehand.frame = self.view.bounds;

        [backScene addSubview:minutehand];

        [UIView animateWithDuration:0.5 animations:^{

            backScene.alpha = 1.0;

        }];

    }];

    

    float delay = 2.0;

    [self showScene:^{

        comment.image = comment3;

        comment.alpha = 1.0;

        [UIView animateWithDuration:3.0 animations:^{

            comment.alpha = 0;

            comment.transform = CGAffineTransformMakeTranslation(0, –100);

        } completion:^(BOOL finished) {

            comment.transform = CGAffineTransformIdentity;

        }];

    } afterDelay:delay];

    

    delay += 1.0;

    [self showScene:^{

        hourhand = [[UIImageView alloc] initWithImage:hourhandImg];

        hourhand.frame = self.view.bounds;

        hourhand.transform = CGAffineTransformMakeTranslation(-200, 50);

        secondhand = [[UIImageView alloc] initWithImage:secondhandImg];

        secondhand.frame = self.view.bounds;

        secondhand.transform = CGAffineTransformMakeTranslation(400, 50);

        [backScene addSubview:hourhand];

        [backScene addSubview:secondhand];

        

        [UIView animateWithDuration:1.0 animations:^{

            secondhand.transform = CGAffineTransformIdentity;

            hourhand.transform = CGAffineTransformIdentity;

        }];

    } afterDelay:delay];

    

    delay += 1.5;

    [self showScene:^{

        [UIView animateWithDuration:1.0 animations:^{

            hourhand.center = CGPointMake(300, –100);

            minutehand.center = CGPointMake(150, –100);

            secondhand.center = CGPointMake(70, –100);

        } completion:^(BOOL finished) {

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

                [self showScene:^{

                    hourhand.alpha = (i % 2) * 1.0;

                    secondhand.alpha = (i % 2) * 1.0;

                    minutehand.alpha = (i % 2) * 1.0;

                } afterDelay: i* 0.2];

            }

        }];

    } afterDelay:delay];

}

– (void)scene3

{

    // 時計縮小

    [UIView animateWithDuration:2.0 animations:^{

        clock.transform = CGAffineTransformIdentity;

        clock.center = CGPointMake(250, 160);

    }];

    

    // 時計アラーム音

    [self playSound:@”jiriri”];

    

    // ジリリリリー

    gyurugyuru = [[UIImageView alloc] initWithImage:gyurugyuruImg];

    gyurugyuru.frame = self.view.bounds;

    [self.view addSubview:gyurugyuru];

    CABasicAnimation *gyuru = [CABasicAnimation animationWithKeyPath:@”transform.translation.y”];

    gyuru.fromValue = [NSNumber numberWithDouble:10];

    gyuru.toValue = [NSNumber numberWithDouble: –10];

    gyuru.autoreverses = YES;

    gyuru.repeatCount = 8;

    gyuru.duration = 0.5;

    gyuru.fillMode = kCAFillModeForwards;

    gyuru.removedOnCompletion = NO;

    [gyurugyuru.layer addAnimation:gyuru forKey:@”gyuru”];

    

    

    CABasicAnimation *ziri = [CABasicAnimation animationWithKeyPath:@”transform.rotation.z”];

    ziri.fromValue = [NSNumber numberWithDouble:M_PI * 0.01];

    ziri.toValue = [NSNumber numberWithDouble: –M_PI * 0.01];

    ziri.autoreverses = YES;

    ziri.repeatCount = 40;

    ziri.duration = 0.1;

    ziri.fillMode = kCAFillModeForwards;

    ziri.removedOnCompletion = NO;

    [clock.layer addAnimation:ziri forKey:@”ziri”];

    

    

    float delay = 1.5;

    [self showScene:^{

        [UIView animateWithDuration:2.0 animations:^{

            gyurugyuru.center = CGPointMake(550, 0);

        }];

    } afterDelay:delay];

}

– (void)scene4

{

    // 背景変更

    [UIView animateWithDuration:0.5 animations:^{

        backScene.alpha = 0;

        clock.alpha = 0;

        gyurugyuru.alpha = 0;

        gyurugyuru.transform = CGAffineTransformMakeScale(0.4, 0.4);

    } completion:^(BOOL finished) {

        backScene.image = backSceneImg;

        gyurugyuru.center = CGPointMake(100, 160);

        [UIView animateWithDuration:0.5 animations:^{

            backScene.alpha = 1.0;

            rabitbody.alpha = 1.0;

            ball.alpha = 1.0;

            gyurugyuru.alpha = 1.0;

        }];

    }];

    

    float delay = 1.5;

    [self showScene:^{

        [UIView animateWithDuration:2.0 animations:^{

            gyurugyuru.center = rabitbody.center;

            gyurugyuru.transform = CGAffineTransformMakeScale(0.1, 0.1);

        }];

    } afterDelay:delay];

    

    

    delay += 2.0;

    [self showScene:^{

        [UIView animateWithDuration:0.2 animations:^{

            gyurugyuru.transform = CGAffineTransformIdentity;

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:0.5 animations:^{

                gyurugyuru.transform = CGAffineTransformMakeScale(0.05, 0.05);;

            } completion:^(BOOL finished) {

                gyurugyuru.alpha = 0;

            }];

        }];

    } afterDelay:delay];

    

    

    delay += 0.5;

    // コメント「おなかすいた~。」

    [self showScene:^{

        comment.image = comment4;

        rabitface.image = rabitface2Img;

        [UIView animateWithDuration:0.2 animations:^{

            comment.alpha = 0.8;

        } completion:^(BOOL finished) {

            

            // おなかがグー

            [self playSound:@”gooo”];

            

            [UIView animateWithDuration:2.0 animations:^{

                comment.alpha = 0;

                comment.center = CGPointMake(comment.center.x100, comment.center.y);

                rabitbody.center = CGPointMake(rabitbody.center.x200, rabitbody.center.y);

            }];

        }];

    } afterDelay:delay];

    

    

    delay += 2.0;

    [self showScene:^{

        // 終わりの音

        [self playSound:@”whistle”];

    } afterDelay:delay];

}

– (void)prepareSound

{

    NSString *path = [[NSBundle mainBundle] pathForResource:@”kick” ofType:@”mp3″];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];

    [player prepareToPlay];

}

#pragma – mark TLR Create Helper

– (void)showScene:(void (^)(void))block afterDelay:(NSTimeInterval)delay

{

    [self performSelector:@selector(executeBlock:) withObject:block afterDelay:delay];

}

– (void)executeBlock:(void (^)(void))block

{

    block();

}

– (UILabel*)createWord:(NSString*)word size:(int)size

{

    OutLineLabel *label = [[OutLineLabel alloc] init];

    label.font = [UIFont fontWithName:@”Chalkduster” size:30];

    label.text = word;

    label.backgroundColor = [UIColor clearColor];

    [label sizeToFit];

    return label;

}

– (void)createOpening

{

    UIImage *rocket = [UIImage imageNamed:@”rocket_back.jpg”];

    startView = [[UIImageView alloc] initWithImage:rocket];

    [self.view addSubview:startView];

    

    UILabel *l = [self createWord:@” Start \n\n Tokyo Little Rocket\n 06″ size:30];

    l.numberOfLines = 0;

    l.textColor = [UIColor blueColor];

    l.frame = CGRectMake(0, 0, 500, 320);

    l.center = startView.center;

    [startView addSubview:l];

}

– (void)createEnding

{

    UIImage *rocket = [UIImage imageNamed:@”rocket_back.jpg”];

    UIImageView *endView = [[UIImageView alloc] initWithImage:rocket];

    endView.frame = CGRectMake(0, –340, 500, 320);

    [self.view addSubview:endView];

    

    UILabel *l = [self createWord:@” End \n\n Tokyo Little Rocket\n 06″ size:30];

    l.numberOfLines = 0;

    l.textColor = [UIColor redColor];

    l.frame = CGRectMake(0, 0, 500, 320);

    [endView addSubview:l];

    [UIView animateWithDuration:1 animations:^{

        endView.center = CGPointMake(250, 160);

    }];

}

– (void)playSound:(NSString*)name

{

    if ([player isPlaying]) {

        [player stop];

    }

    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@”mp3″];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];

    [player prepareToPlay];

    [player play];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end