iPhoneいないいないばぁ

タップでいないいないばぁ。という感じのシンプルなiPhoneアプリのサンプルを描いてみます。


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

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (strong, nonatomic) UIImageView *face;

@property (strong, nonatomic) UIImageView *handR;

@property (strong, nonatomic) UIImageView *handL;

@property (strong, nonatomic) UIImage *smile;

@property (strong, nonatomic) UIImage *hoho;

@property (strong, nonatomic) UIImage *cry;

@property (strong, nonatomic) UILabel *label;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor greenColor];

    

    [self createFace];

    [self createHands];

    [self createLabel];

}

– (void)createFace

{

    UIImage *image = [UIImage imageNamed:@”peekaboo_face”];

    self.face = [[UIImageView alloc] initWithImage:image];

    self.face.frame = CGRectMake(30, 100, 250, 250);

    [self.view addSubview:self.face];

}

– (void)createHands

{

    UIImage *imageR = [UIImage imageNamed:@”peekaboo_handR”];

    self.handR = [[UIImageView alloc] initWithImage:imageR];

    self.handR.frame = CGRectMake(0, 0, 180, 180);

    [self.view addSubview:self.handR];

    self.handR.layer.anchorPoint = CGPointMake(0, 1.0);

    self.handR.layer.position = CGPointMake(0, 350);

    UIImage *imageL = [UIImage imageNamed:@”peekaboo_handL”];

    self.handL = [[UIImageView alloc] initWithImage:imageL];

    self.handL.frame = CGRectMake(0, 0, 180, 180);

    [self.view addSubview:self.handL];

    self.handL.layer.anchorPoint = CGPointMake(1.0, 1.0);

    self.handL.layer.position = CGPointMake(320, 350);

}

– (void)createLabel

{

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 400, 220, 40)];

    self.label.backgroundColor = [UIColor brownColor];

    self.label.text = @”いない,いな~;

    self.label.textColor = [UIColor whiteColor];

    self.label.font = [UIFont systemFontOfSize:25];

    self.label.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:self.label];

    

}

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

{

    NSArray *faceTypes = @[

                           [UIImage imageNamed:@”peekaboo_smile”],

                           [UIImage imageNamed:@”peekaboo_hoho”],

                           [UIImage imageNamed:@”peekaboo_cry”]

                           ];

    UIImageView *faceType = [[UIImageView alloc] initWithImage:faceTypes[arc4random()%3]];

    faceType.frame = CGRectMake(0, 0, 130, 130);

    faceType.center = CGPointMake(CGRectGetMidX(self.face.bounds), CGRectGetMidY(self.face.bounds) + 30);

    

    [self.face.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        [obj removeFromSuperview];

    }];

    

    [self.face addSubview:faceType];

    

    

    self.label.text = @”ばぁ!;

    [UIView animateWithDuration:0.3 animations:^{

        self.handR.transform = CGAffineTransformMakeRotation(-M_PI/3.0);

        self.handL.transform = CGAffineTransformMakeRotation(M_PI/3.0);

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:1.5 animations:^{

            self.handR.transform = CGAffineTransformIdentity;

            self.handL.transform = CGAffineTransformIdentity;

        } completion:^(BOOL finished) {

            self.label.text = @”いない,いな~;

        }];

    }];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end