ネコをライオンに変身させて遊ぶシンプルなiPhoneアプリを描いてみます。
動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#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]
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = UIColorHex(0xffd879);
[self setupFace];
[self setupSkinTypeLion];
[self setupSkinTypeCat];
}
– (void)setupFace
{
UIImage *img = [UIImage imageNamed:@”animal_face”];
UIImageView *face = [[UIImageView alloc] initWithImage:img];
face.frame = CGRectMake(0, 50, 240, 190);
[self.view addSubview:face];
}
– (void)setupSkinTypeLion
{
UIImage *img = [UIImage imageNamed:@”animal_lion”];
UIImageView *skin = [[UIImageView alloc] initWithImage:img];
skin.frame = CGRectMake(0, 0, 240, 190);
skin.layer.anchorPoint = CGPointMake(0, 0);
skin.layer.position = CGPointMake(240, 50);
[self.view addSubview:skin];
skin.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(flip:)];
[skin addGestureRecognizer:tap];
}
– (void)setupSkinTypeCat
{
UIImage *img = [UIImage imageNamed:@”animal_cat”];
UIImageView *skin = [[UIImageView alloc] initWithImage:img];
skin.frame = CGRectMake(0, 0, 240, 190);
skin.layer.anchorPoint = CGPointMake(0, 0);
skin.layer.position = CGPointMake(240, 50);
[self.view addSubview:skin];
skin.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(flip:)];
[skin addGestureRecognizer:tap];
}
– (void)flip:(UITapGestureRecognizer*)gr
{
if (CATransform3DIsIdentity(gr.view.layer.transform)) {
[UIView animateWithDuration:0.5 animations:^{
gr.view.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
} completion:^(BOOL finished) {
[self.view addSubview:gr.view];
}];
} else {
[UIView animateWithDuration:0.5 animations:^{
gr.view.layer.transform = CATransform3DIdentity;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
gr.view.transform = CGAffineTransformMakeTranslation(0, –200);
} completion:^(BOOL finished) {
[self.view insertSubview:gr.view atIndex:0];
[UIView animateWithDuration:0.2 animations:^{
gr.view.transform = CGAffineTransformIdentity;
}];
}];
}];
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end