すいかをタップすると、パカッと半分に割れるというかんじのiPhoneゲームアプリを書いてみます。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
UIView *waterMelon;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createBackground];
[self createWaterMelon];
}
– (void)createBackground
{
self.view.backgroundColor = [self color:3];
UIView *ground = [[UIView alloc] initWithFrame:CGRectMake(0, 260, 568, 60)];
ground.backgroundColor = [self color:0];
[self.view addSubview:ground];
}
– (void)createWaterMelon
{
float s = 100;
UIBezierPath *path = [UIBezierPath bezierPath];
for (int i=0; i<5; i++) {
[path moveToPoint:CGPointMake(i * 10 + 30, 3)];
CGPoint cp = CGPointMake(s/4.0 * i, s/2.0);
[path addQuadCurveToPoint:CGPointMake(i * 10 + 30, s-3) controlPoint:cp];
}
CAShapeLayer *sl = [CAShapeLayer layer];
sl.fillColor = [UIColor clearColor].CGColor;
sl.strokeColor = [UIColor blackColor].CGColor;
sl.lineWidth = 4;
sl.path = path.CGPath;
waterMelon = [[UIView alloc] initWithFrame:CGRectMake(260, 180, s, s)];
waterMelon.backgroundColor = [self color:1];
waterMelon.layer.cornerRadius = s/2.0;
[waterMelon.layer addSublayer:sl];
[self.view addSubview:waterMelon];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[waterMelon addGestureRecognizer:tap];
}
– (void)tap:(UITapGestureRecognizer*)gr
{
UIGraphicsBeginImageContext(waterMelon.frame.size);
[waterMelon.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
float x = waterMelon.frame.origin.x;
float y = waterMelon.frame.origin.y;
float w = waterMelon.frame.size.width;
float h = waterMelon.frame.size.height;
UIImageView *left = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w/2.0, h)];
left.layer.masksToBounds = YES;
left.contentMode = UIViewContentModeScaleAspectFill;
CGImageRef leftImgRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, 0, w/2.0, h));
left.image = [UIImage imageWithCGImage:leftImgRef];
CGImageRelease(leftImgRef);
left.layer.anchorPoint = CGPointMake(0.5, 1.0);
left.layer.position = CGPointMake(left.layer.position.x, left.layer.position.y + left.frame.size.height/2.0);
[self.view addSubview:left];
UIImageView *right = [[UIImageView alloc] initWithFrame:CGRectMake(x + w/2.0, y, w/2.0, h)];
right.layer.masksToBounds = YES;
right.contentMode = UIViewContentModeScaleAspectFill;
CGImageRef rightImgRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(w/2.0, 0, w/2.0, h));
right.image = [UIImage imageWithCGImage:rightImgRef];
CGImageRelease(rightImgRef);
right.layer.anchorPoint = CGPointMake(0.5, 1.0);
right.layer.position = CGPointMake(right.layer.position.x, right.layer.position.y + right.frame.size.height/2.0);
[self.view addSubview:right];
[waterMelon removeFromSuperview];
[UIView animateWithDuration:0.5 animations:^{
left.transform = CGAffineTransformMakeRotation(-M_PI/3.0);
right.transform = CGAffineTransformMakeRotation(M_PI/3.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
left.alpha = 0;
right.alpha = 0;
} completion:^(BOOL finished) {
[left removeFromSuperview];
[right removeFromSuperview];
[self createWaterMelon];
float x = 100 * (arc4random() % 3) + 100;
waterMelon.center = CGPointMake(x, waterMelon.center.y);
waterMelon.transform = CGAffineTransformMakeTranslation(0, –400);
[UIView animateWithDuration:0.5 animations:^{
waterMelon.transform = CGAffineTransformIdentity;
}];
}];
}];
}
#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]
– (UIColor*)color:(int)i
{
switch (i) {
case 0:
return UIColorHex(0xEBE6A5);
case 1:
return UIColorHex(0x346E3B);
case 2:
return UIColorHex(0x51D97D);
case 3:
return UIColorHex(0x1FD6E8);
case 4:
return UIColorHex(0x078694);
default:
break;
}
return nil;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end