iphoneそば

年末なので、そば食べてる感じでiPhoneアプリのサンプルコードを描いてみます。


今回使った画像


動かすとこんな感じです

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (weak, nonatomic) UIImageView *soba;

@property (weak, nonatomic) UIImageView *face;

@property (weak, nonatomic) UIImageView *hashi;

@property (weak, nonatomic) UIImageView *topping;

@end

@implementation ViewController

@synthesize soba;

@synthesize face;

@synthesize hashi;

@synthesize topping;

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];

    [self createFace];

    [self createHashi];

    [self sobaPlease];

    [self toppingPlease];

    [self setupSwipe];

}

– (void)createFace

{

    UIImage *img = [UIImage imageNamed:@”face02″];

    UIImageView *v = [[UIImageView alloc] initWithImage:img];

    face = v;

    face.center = CGPointMake(160, 160);

    [self.view addSubview:face];

}

– (void)createHashi

{

    UIImage *img = [UIImage imageNamed:@”hashi”];

    UIImageView *v = [[UIImageView alloc] initWithImage:img];

    hashi = v;

    hashi.center = CGPointMake(230, 320);

    [self.view addSubview:hashi];

}

– (void)sobaPlease

{

    UIImage *img = [UIImage imageNamed:@”soba”];

    UIImageView *v = [[UIImageView alloc] initWithImage:img];

    soba = v;

    soba.center = CGPointMake(160, 380);

    [self.view addSubview:soba];

}

– (void)toppingPlease

{

    UIImage *img = [UIImage imageNamed:@”ebi”];

    UIImageView *v = [[UIImageView alloc] initWithImage:img];

    topping = v;

    topping.center = CGPointMake(160, 330);

    [self.view addSubview:topping];

}

– (void)setupSwipe

{

    UISwipeGestureRecognizer *up = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(eat:)];

    up.direction = UISwipeGestureRecognizerDirectionUp;

    [self.view addGestureRecognizer:up];

    

    

    int directions[] = {UISwipeGestureRecognizerDirectionLeft, UISwipeGestureRecognizerDirectionRight};

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

        UISwipeGestureRecognizer *LR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeTopping:)];

        LR.direction = directions[i];

        [self.view addGestureRecognizer:LR];

    }

}

– (void)eat:(UISwipeGestureRecognizer*)gr

{

    face.image = [UIImage imageNamed:@”face01″];

    [UIView animateWithDuration:1.0 animations:^{

        self.hashi.transform = CGAffineTransformMakeTranslation(0, –110);

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:1.0 delay:2.0 options:0 animations:^{

            self.hashi.transform = CGAffineTransformIdentity;

            face.image = [UIImage imageNamed:@”face02″];

        } completion:^(BOOL finished) {

        }];

    }];

}

– (void)changeTopping:(UISwipeGestureRecognizer*)gr

{

    CGPoint p = [gr locationInView:self.view];

    

    if (CGRectContainsPoint(soba.frame, p)) {

        NSArray *types = @[@”ebi”, @”egg”, @”kitune”];

        UIImage *img = [UIImage imageNamed:[types objectAtIndex:arc4random()%3]];

        UIImageView *newTopping = [[UIImageView alloc] initWithImage:img];

        newTopping.center = self.topping.center;

        [self.view addSubview:newTopping];

        

        newTopping.transform = CGAffineTransformMakeTranslation(250, 0);

        [UIView animateWithDuration:0.5 animations:^{

            self.topping.transform = CGAffineTransformMakeTranslation(-250, 0);

            newTopping.transform = CGAffineTransformIdentity;

        } completion:^(BOOL finished) {

            [self.topping removeFromSuperview];

            self.topping = newTopping;

        }];

        

    }

}

@end