iPhoneパクパク

お皿にのっている丸をただひたすらに食べ続けるといったiPhoneアプリを描いてみます。


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

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (strong, nonatomic) UIView *dish;

@property (strong, nonatomic) UIView *eater;

@property (strong, nonatomic) NSMutableArray *foods;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor whiteColor];

    

    [self createFloor];

    [self createDish];

    [self createEater];

    [self createFoods];

}

– (void)createFloor

{

    UIView *floor = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 568, 150)];

    floor.layer.borderColor = [UIColor brownColor].CGColor;

    floor.layer.borderWidth = 10;

    

    [self.view addSubview:floor];

    

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

        CALayer *l = [CALayer layer];

        l.backgroundColor = [UIColor brownColor].CGColor;

        l.frame = CGRectMake(10 * i, –10, 5, 200);

        l.transform = CATransform3DMakeRotation(M_PI * 0.2,0,0,1);

        [floor.layer addSublayer:l];

    }

}

– (void)createDish

{

    self.dish = [[UIView alloc] initWithFrame:CGRectMake(150, 200, 100, 100)];

    self.dish.backgroundColor = [UIColor whiteColor];

    self.dish.layer.cornerRadius = 50;

    self.dish.layer.transform = CATransform3DMakeRotation(M_PI*0.3, 1, 0, 0);

    self.dish.layer.zPosition = 50;

    [self.view addSubview:self.dish];

}

– (void)createEater

{

    self.eater = [[UIView alloc] initWithFrame:CGRectMake(250, 150, 120, 120)];

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    

    [path addArcWithCenter:CGPointMake(60, 60) radius:30 startAngle:-M_PI endAngle:M_PI clockwise:YES];

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = [UIColor clearColor].CGColor;

    sl.strokeColor = [UIColor yellowColor].CGColor;

    sl.lineWidth = 60;

    sl.strokeEnd = 0.85;

    sl.path = path.CGPath;

    [self.eater.layer addSublayer:sl];

    [self.view addSubview:self.eater];

    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(eat)];

    [self.eater addGestureRecognizer:tap];

}

– (void)createFoods

{

    if (!self.foods) {

        self.foods = [[NSMutableArray alloc] init];

    }

    

    CGPoint center[3] = {CGPointMake(185, 255), CGPointMake(215, 255), CGPointMake(200, 235),};

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

        UIView *food = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

        food.backgroundColor = [UIColor redColor];

        food.center = center[i];

        food.layer.zPosition = 100;

        food.layer.cornerRadius = 10;

        [self.view addSubview:food];

        

        food.transform = CGAffineTransformMakeTranslation(0, –300);

        [UIView animateWithDuration:0.5 animations:^{

            food.transform = CGAffineTransformIdentity;

        }];

        

        [self.foods addObject:food];

    }

}

– (void)eat

{

    CABasicAnimation *paku = [CABasicAnimation animationWithKeyPath:@”strokeEnd”];

    paku.duration = 0.25;

    paku.repeatCount = 3;

    paku.autoreverses = YES;

    paku.toValue = @1;

    paku.fromValue = @0.85;

    

    CAShapeLayer *eaterSL = [self.eater.layer.sublayers objectAtIndex:0];

    [eaterSL addAnimation:paku forKey:nil];

    

    [UIView animateWithDuration:0.5 animations:^{

        [self.foods[0] setAlpha:0];

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.5 animations:^{

            [self.foods[1] setAlpha:0];

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:0.5 animations:^{

                [self.foods[2] setAlpha:0];

            } completion:^(BOOL finished) {

                

                // reset

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

                    UIView *food = self.foods[i];

                    food.alpha = 1.0;

                    food.transform = CGAffineTransformMakeTranslation(0, –300);

                    double delayInSeconds = 0.2 * i;

                    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));

                    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

                        [UIView animateWithDuration:0.3 animations:^{

                            food.transform = CGAffineTransformIdentity;

                        }];

                    });

                }

            }];

        }];

    }];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end