太陽と雲をよういして、くもの陰から四角が出ないように動かそう。というiPhoneアプリを作ってみる。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
float velocity;
UIView *cloud;
UIView *player;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [self color:4];
velocity = 1.0f;
[self createSun];
[self createCloud];
[self createPlayer];
[self start];
}
– (void)createSun
{
UIView *sun = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
sun.backgroundColor = [self color:2];
sun.layer.cornerRadius = 40;
sun.layer.masksToBounds = NO;
[self.view addSubview:sun];
UIBezierPath *path = [UIBezierPath bezierPath];
for (int i=0; i<16; i++) {
float angle = (M_PI/8.0) * i;
[path moveToPoint:CGPointMake(42 * cos(angle) + 40, 42 * sin(angle) + 40)];
angle += M_PI / 16.0;
[path addLineToPoint:CGPointMake(55 * cos(angle) + 40, 55 * sin(angle) + 40)];
angle += M_PI / 16.0;
[path addLineToPoint:CGPointMake(42 * cos(angle) + 40, 42 * sin(angle) + 40)];
[path closePath];
}
CAShapeLayer *sl = [CAShapeLayer layer];
sl.fillColor = [self color:2].CGColor;
sl.path = path.CGPath;
[sun.layer addSublayer:sl];
}
– (void)createCloud
{
cloud = [[UIView alloc] initWithFrame:CGRectMake(160, 80, 100, 50)];
cloud.backgroundColor = [UIColor clearColor];
[self.view addSubview:cloud];
CGPoint p[] = {CGPointMake(0, 10), CGPointMake(20, 0), CGPointMake(40, 20),CGPointMake(60, 10), CGPointMake(80, 20),};
for (int i=0; i<5; i++) {
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(p[i].x, p[i].y, 50, 50)];
v.layer.cornerRadius = 25;
v.backgroundColor = [self color:3];
[cloud addSubview:v];
}
cloud.layer.shadowOpacity = 0.2;
cloud.layer.shadowRadius = 4.0;
cloud.layer.shadowOffset = CGSizeMake(3, 140);
}
– (void)createPlayer
{
player = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
player.backgroundColor = [self color:1];
player.layer.cornerRadius = 5;
player.center = CGPointMake(cloud.center.x, 260);
[self.view insertSubview:player belowSubview:cloud];
UIBezierPath *path = [UIBezierPath bezierPath];
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(5, 10, 10, 10)]];
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(25, 10, 10, 10)]];
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 30, 20, 5)]];
CAShapeLayer *sl = [CAShapeLayer layer];
sl.fillColor = [UIColor blackColor].CGColor;
sl.strokeColor = [UIColor blackColor].CGColor;
sl.path = path.CGPath;
[player.layer addSublayer:sl];
}
– (void)smile
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(5, 10, 10, 10)]];
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(25, 10, 10, 10)]];
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 30, 20, 5)]];
CAShapeLayer *sl = [player.layer.sublayers objectAtIndex:0];
sl.lineWidth = 0;
sl.path = path.CGPath;
}
– (void)xEye
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(5, 5)];
[path addLineToPoint:CGPointMake(15, 15)];
[path moveToPoint:CGPointMake(5, 15)];
[path addLineToPoint:CGPointMake(15, 5)];
[path moveToPoint:CGPointMake(25, 5)];
[path addLineToPoint:CGPointMake(35, 15)];
[path moveToPoint:CGPointMake(25, 15)];
[path addLineToPoint:CGPointMake(35, 5)];
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 30, 20, 5)]];
CAShapeLayer *sl = [player.layer.sublayers objectAtIndex:0];
sl.lineWidth = 3;
sl.path = path.CGPath;
}
– (void)start
{
[NSTimer scheduledTimerWithTimeInterval:2.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
– (void)tick:(NSTimer*)sender
{
// cloud
if (arc4random() % 50 == 0) {
velocity = (-1) * velocity;
}
if (!CGRectIntersectsRect(cloud.frame, CGRectMake(80, 0, 460, 320))) {
if (cloud.center.x < 140) {
velocity = 1;
} else {
velocity = –1;
}
}
cloud.center = CGPointMake(cloud.center.x + velocity, cloud.center.y);
// player
switch (player.tag) {
case 1:
player.center = CGPointMake(player.center.x + 1, player.center.y);
break;
case 2:
player.center = CGPointMake(player.center.x – 1, player.center.y);
break;
}
if (CGRectContainsPoint(cloud.frame, CGPointMake(player.center.x, 100))) {
// smile
[self smile];
} else {
// x eye
[self xEye];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.view];
if (player.center.x < p.x) {
player.tag = 1;
} else {
player.tag = 2;
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
player.tag = 0;
}
#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(0xFFFFFF);
case 1:
return UIColorHex(0xFFA34B);
case 2:
return UIColorHex(0xFF5E4C);
case 3:
return UIColorHex(0xD7F7D9);
case 4:
return UIColorHex(0x48D9DE);
default:
break;
}
return nil;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end