パネルをパキパキ割って遊ぶ単純なiPhoneゲームアプリを書いてみます。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
UIView *plateLeft;
UIView *plateRight;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createBackground];
[self createPlates:[self color:4]];
[self showPlates];
}
– (void)createBackground
{
self.view.backgroundColor = [self color:0];
UILabel *l = [[UILabel alloc] init];
l.text = @”Brakable”;
l.font = [UIFont fontWithName:@”AvenirNext-UltraLight” size:60];
l.textColor = [UIColor whiteColor];
[l sizeToFit];
l.center = CGPointMake(160, 160);
l.backgroundColor = [UIColor clearColor];
[self.view addSubview:l];
}
– (void)createPlates:(UIColor*)color
{
float w = 100;
float h = 100;
plateLeft = [[UIView alloc] initWithFrame:CGRectMake(110, 250, w, h)];
plateLeft.backgroundColor = [UIColor clearColor];
[self.view addSubview:plateLeft];
plateRight = [[UIView alloc] initWithFrame:CGRectMake(110, 250, w, h)];
[self.view addSubview:plateRight];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(w/2.0, 0)];
int count = arc4random() % 4 + 2;
for (int i=0; i<count; i++) {
int size = arc4random() % 20 – 10;
[path addLineToPoint:CGPointMake(w/2.0 + 3 * size, h/count * (i + 1))];
}
[path addLineToPoint:CGPointMake(0, h)];
[path closePath];
CAShapeLayer *sl = [CAShapeLayer layer];
sl.fillColor = color.CGColor;
sl.path = path.CGPath;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRect:plateRight.bounds];
[maskPath appendPath:path];
CAShapeLayer *maskSl = [CAShapeLayer layer];
maskSl.fillRule = kCAFillRuleEvenOdd;
maskSl.fillColor = color.CGColor;
maskSl.path = maskPath.CGPath;
[plateLeft.layer addSublayer:sl];
[plateRight.layer addSublayer:maskSl];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(breakPlates)];
[plateRight addGestureRecognizer:tap];
}
– (void)showPlates
{
plateRight.transform = CGAffineTransformMakeTranslation(0, –500);
plateLeft.transform = CGAffineTransformMakeTranslation(0, –500);
[UIView animateWithDuration:0.5 animations:^{
plateRight.transform = CGAffineTransformIdentity;
plateLeft.transform = CGAffineTransformIdentity;
}];
}
– (void)breakPlates
{
[UIView animateWithDuration:0.1 animations:^{
plateRight.transform = CGAffineTransformMakeTranslation(20, 0);
plateLeft.transform = CGAffineTransformMakeTranslation(-20, 0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
plateRight.alpha = 0.6;
plateLeft.alpha = 0.6;
} completion:^(BOOL finished) {
[plateRight removeFromSuperview];
[plateLeft removeFromSuperview];
[self createPlates:[self color:4]];
}];
}];
}
#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(0xEF732D);
case 1:
return UIColorHex(0xFFBA9F);
case 2:
return UIColorHex(0xFAFAFA);
case 3:
return UIColorHex(0x77929C);
case 4:
return UIColorHex(0xA1C8C9);
default:
break;
}
return nil;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end