たなばたなので、笹と星を使って、1から7までの個数を学べるような子供向けのiPhoneアプリを作ってみます。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
ポイント
背景に笹っぽいものをCAShapeLayerで書いて、星、短冊をUIViewで用意します。短冊の中には、白い星を表示しておいて、そこに黄色い星を指で持ってくるようなゲームにしました。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
UIView *tanzaku;
int count;
NSMutableArray *holes;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self createBamboo];
count = arc4random() % 7 + 1;
[self createTanzaku];
[self createStars];
}
– (void)createBamboo
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(20, 568)];
[path addQuadCurveToPoint:CGPointMake(260, 160) controlPoint:CGPointMake(0, 100)];
[path moveToPoint:CGPointMake(25, 400)];
[path addQuadCurveToPoint:CGPointMake(70, 385) controlPoint:CGPointMake(30, 380)];
[path moveToPoint:CGPointMake(25, 400)];
[path addQuadCurveToPoint:CGPointMake(70, 385) controlPoint:CGPointMake(30, 380)];
CAShapeLayer *sl = [[CAShapeLayer alloc] init];
sl.fillColor = [UIColor clearColor].CGColor;
sl.strokeColor = [UIColor greenColor].CGColor;
sl.lineWidth = 2;
sl.path = path.CGPath;
[self.view.layer addSublayer:sl];
// leaf
CALayer *leafa = [self createLeaf];
leafa.position = CGPointMake(260, 155);
CALayer *leafb = [self createLeaf];
leafb.position = CGPointMake(260, 155);
leafb.transform = CATransform3DMakeRotation(M_PI/4.0, 0, 0, 1);
CALayer *leafc = [self createLeaf];
leafc.position = CGPointMake(255, 155);
leafc.transform = CATransform3DMakeRotation(-M_PI/4.0, 0, 0, 1);
CALayer *leafd = [self createLeaf];
leafd.position = CGPointMake(70, 380);
CALayer *leafe = [self createLeaf];
leafe.position = CGPointMake(70, 380);
leafe.transform = CATransform3DMakeRotation(M_PI/4.0, 0, 0, 1);
CALayer *leaff = [self createLeaf];
leaff.position = CGPointMake(65, 380);
leaff.transform = CATransform3DMakeRotation(-M_PI/4.0, 0, 0, 1);
}
– (CALayer*)createLeaf
{
UIBezierPath *leafpath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 40, 10)];
CAShapeLayer *leafsl = [[CAShapeLayer alloc] init];
leafsl.fillColor = [UIColor greenColor].CGColor;
leafsl.path = leafpath.CGPath;
[self.view.layer addSublayer:leafsl];
return leafsl;
}
– (void)createStars
{
for (int i=0; i<count; i++) {
float x = arc4random() % 260 + 30;
float y = arc4random() % 160 + 30;
UIView *star = [self createStar];
star.center = CGPointMake(x, y);
}
}
– (UIView *)createStar
{
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint p[5];
for (int i=0; i<5; i++) {
float x = 20 * cos(M_PI / 2.5 * i – M_PI/2.0) + 20;
float y = 20 * sin(M_PI / 2.5 * i – M_PI/2.0) + 20;
p[i] = CGPointMake(x, y);
}
[path moveToPoint:p[0]];
[path addLineToPoint:p[2]];
[path addLineToPoint:p[4]];
[path addLineToPoint:p[1]];
[path addLineToPoint:p[3]];
[path addLineToPoint:p[0]];
CAShapeLayer *sl = [[CAShapeLayer alloc] init];
sl.fillColor = [UIColor yellowColor].CGColor;
sl.path = path.CGPath;
UIView *star = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[star.layer addSublayer:sl];
[self.view addSubview:star];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[star addGestureRecognizer:pan];
return star;
}
– (void)createTanzaku
{
tanzaku = [[UIView alloc] initWithFrame:CGRectMake(120, 200, 100, 300)];
tanzaku.backgroundColor = [UIColor purpleColor];
[self.view addSubview:tanzaku];
holes = [[NSMutableArray alloc] init];
for (int i=0; i<count; i++) {
UIView *hole = [self createStar];
hole.userInteractionEnabled = NO;
CAShapeLayer *l = [hole.layer.sublayers objectAtIndex:0];
l.fillColor = [UIColor whiteColor].CGColor;
hole.center = CGPointMake(50, 40 * i + 30);
[tanzaku addSubview:hole];
[holes addObject:hole];
}
}
– (void)move:(UIPanGestureRecognizer*)gr
{
CGPoint p = [gr locationInView:self.view];
gr.view.center = p;
if (gr.state == UIGestureRecognizerStateEnded) {
for (UIView *v in holes) {
CGRect check = [tanzaku convertRect:v.frame toView:self.view];
if (CGRectIntersectsRect(check, gr.view.frame)) {
[UIView animateWithDuration:0.3 animations:^{
gr.view.center = CGPointMake(check.origin.x + 20, check.origin.y + 20);
} completion:^(BOOL finished) {
[holes removeObject:v];
if ([holes count] == 0) {
[self restart];
}
}];
}
}
}
}
– (void)restart
{
for (UIView *v in self.view.subviews) {
[v removeFromSuperview];
}
[holes removeAllObjects];
count = arc4random() % 7 + 1;
[self createTanzaku];
[self createStars];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end