汚れをタッチで落とすゲームのラフを作ってみた。
(XcodeのiOS6 Simulatorで試しています。)
ポイント
・汚れは、CALayerの集合で描画、タッチで集合から取り除いていく
・CAGradientLayerを使ったらなんか浮いてる感じになった。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *stains;
@end
@implementation ViewController
@synthesize stains;
– (void)viewDidLoad
{
[super viewDidLoad];
// Title
UILabel *title = [[UILabel alloc] init];
title.text = @”clearn!”;
title.font = [UIFont boldSystemFontOfSize:40];
title.textColor = [UIColor greenColor];
[title sizeToFit];
title.center = CGPointMake(80, 420);
[self.view addSubview:title];
// 汚れを描画
for (int i=0; i<10; i++) {
[self createStain];
}
}
– (void)createStain
{
// 汚れの場所をランダムに
float x = arc4random() % 300;
float y = arc4random() % 400;
UIView *stain = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
stain.center = CGPointMake(x, y);
// フレームの中に水玉で汚れを表現
for (int i=0; i<20; i++) {
float x = arc4random() % 40;
float y = arc4random() % 40;
float size = arc4random() % 20 + 10;
// なんとなくCALayerにグラデーション
CAGradientLayer *s = [CAGradientLayer layer];
UIColor *color1 = [UIColor colorWithWhite:0.2 alpha:0.9];
UIColor *color2 = [UIColor colorWithWhite:0.3 alpha:0.9];
s.colors = [NSArray arrayWithObjects:(id)color1.CGColor, color2.CGColor, nil];
s.frame = CGRectMake(x, y, size, size);
s.cornerRadius = size * 0.5;
[stain.layer addSublayer:s];
}
[self.view addSubview:stain];
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
CGPoint p = [t locationInView:self.view];
// タッチで動かしたところの汚れを取っていく。
for (UIView *stain in self.view.subviews) {
if (CGRectContainsPoint(stain.frame, p)) {
[stain.layer.sublayers.lastObject removeFromSuperlayer];
}
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end