扇風機の風を使って、上から落ちてくるボールを的にあててみましょう。
遠くにボールを飛ばしたいときは、ゆびを素早く、
近くにボールを落としたいときは、ゆびをゆっくり、
扇風機の上でゆびを動かしてボールをコントロール。
というゲームのサンプルをつくってみました。
ポイント
長方形のUIViewを4つ重ね、それぞれに角度を付けて羽にしています。
さらに、扇風機自体をCATransform3Dでy軸周りの回転を少し付けて、
横を向いてる感を出してみました。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
NSTimer *timer;
UIView *fan;
UIView *goal;
UILabel *scoreBoard;
float velocity;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createFan];
[self startTimer];
[self createGoal];
[self createScoreBoard];
}
– (void)createFan
{
fan = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 120, 120)];
fan.backgroundColor = [UIColor lightGrayColor];
fan.layer.borderColor = [UIColor whiteColor].CGColor;
fan.layer.borderWidth = 5.0;
fan.layer.cornerRadius = 60;
fan.layer.transform = CATransform3DMakeRotation(M_PI * 0.3, 0, 1, 0);
[self.view addSubview:fan];
for (int i=0; i<4; i++) {
UIView *blade = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
blade.center = CGPointMake(60, 60);
blade.transform = CGAffineTransformMakeRotation(i * M_PI * 0.25);
blade.backgroundColor = [UIColor whiteColor];
blade.layer.cornerRadius = 5.0;
[fan addSubview:blade];
}
UIView *baseA = [[UIView alloc] initWithFrame:CGRectMake(30, 380, 20, 70)];
baseA.backgroundColor = [UIColor whiteColor];
baseA.layer.cornerRadius = 5.0;
[self.view addSubview:baseA];
UIView *baseB = [[UIView alloc] initWithFrame:CGRectMake(20, 440, 60, 20)];
baseB.backgroundColor = [UIColor whiteColor];
baseB.layer.cornerRadius = 5.0;
[self.view addSubview:baseB];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[fan addGestureRecognizer:pan];
}
– (void)pan:(UIPanGestureRecognizer*)gr
{
CGPoint v = [gr velocityInView:self.view];
velocity = hypotf(v.x, v.y) * 0.0001;
}
– (void)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
– (void)tick:(NSTimer*)sender
{
static float time = 0;
time += sender.timeInterval;
if (time > 2) {
time = 0;
UIView *item = [[UIView alloc] initWithFrame:CGRectMake(100, 0, 20, 20)];
item.layer.cornerRadius = 10.0;
item.backgroundColor = [UIColor whiteColor];
item.tag = 10;
[self.view addSubview:item];
}
for (UIView *blade in fan.subviews) {
blade.transform = CGAffineTransformRotate(blade.transform, velocity);
}
for (UIView *item in self.view.subviews) {
if (item.tag == 10) {
float x = item.center.x;
if (item.center.y > 300) {
x += velocity * 100000 / pow(x,2) ;
} else if (item.center.y > 500){
[item removeFromSuperview];
continue;
}
item.center = CGPointMake(x, item.center.y + 1);
// goal check
if (CGRectIntersectsRect(item.frame, goal.frame)) {
// score up
int current = [[scoreBoard.text substringFromIndex:6] intValue];
scoreBoard.text = [NSString stringWithFormat:@”SCORE %04d”, current + 10];
[goal removeFromSuperview];
[self createGoal];
}
}
}
}
– (void)createGoal
{
float x = arc4random() % 150 + 120;
goal = [[UIView alloc] initWithFrame:CGRectMake(x, 430, 40, 30)];
goal.backgroundColor = [UIColor yellowColor];
UILabel *pt = [[UILabel alloc] init];
pt.font = [UIFont boldSystemFontOfSize:22];
pt.text = @”10P”;
pt.textColor = self.view.backgroundColor;
[pt sizeToFit];
pt.backgroundColor = [UIColor clearColor];
pt.center = CGPointMake(20, 15);
[goal addSubview:pt];
[self.view addSubview:goal];
}
– (void)createScoreBoard
{
scoreBoard = [[UILabel alloc] init];
scoreBoard.font = [UIFont boldSystemFontOfSize:20];
scoreBoard.text = @”SCORE 0000″;
scoreBoard.backgroundColor = [UIColor clearColor];
scoreBoard.textColor = [UIColor whiteColor];
[scoreBoard sizeToFit];
scoreBoard.center = CGPointMake(250, 20);
[self.view addSubview:scoreBoard];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end