タップすると、そこに芽が出てくるiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createFarm];
}
– (void)createFarm
{
self.view.backgroundColor = [self color:4];
UIBezierPath *path = [UIBezierPath bezierPath];
for (int i=0; i<100; i++) {
float x = (i % 10) * 40;
float y = (i / 10) * 60 + ((i % 2) ? 20 : 50);
if ((i % 10) == 0)
[path moveToPoint:CGPointMake(x, y)];
else
[path addLineToPoint:CGPointMake(x, y)];
}
CAShapeLayer *l = [CAShapeLayer layer];
l.path = path.CGPath;
l.lineWidth = 3;
l.strokeColor = [self color:3].CGColor;
l.fillColor = [UIColor clearColor].CGColor;
[self.view.layer addSublayer:l];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.view];
[self createSprout:p];
}
– (void)createSprout:(CGPoint)p
{
UIView *seed = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
seed.center = p;
seed.backgroundColor = [self color:0];
seed.layer.anchorPoint = CGPointMake(0.5, 1.0);
[self.view addSubview:seed];
[UIView animateWithDuration:1.0 animations:^{
seed.transform = CGAffineTransformMakeScale(1.0, 5.0);
} completion:^(BOOL finished) {
UIView *leaf = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 10)];
leaf.layer.anchorPoint = CGPointMake(1.0, 0.5);
leaf.layer.cornerRadius = 15;
leaf.layer.position = CGPointMake(p.x, p.y – 26);
leaf.backgroundColor = [self color:0];
[self.view addSubview:leaf];
[UIView animateWithDuration:0.5 animations:^{
leaf.transform = CGAffineTransformMakeRotation(M_PI * 0.1);
} completion:^(BOOL finished) {
UIView *leafB = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 10)];
leafB.layer.anchorPoint = CGPointMake(0, 0.5);
leafB.layer.cornerRadius = 15;
leafB.layer.position = CGPointMake(p.x, p.y – 26);
leafB.backgroundColor = [self color:0];
[self.view addSubview:leafB];
[UIView animateWithDuration:0.5 animations:^{
leafB.transform = CGAffineTransformMakeRotation(-M_PI * 0.1);
}];
}];
}];
}
#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:(NSInteger)i
{
switch (i) {
case 0:
return UIColorHex(0x62c933);
case 1:
return UIColorHex(0xFFFFCE);
case 2:
return UIColorHex(0xCFBC8F);
case 3:
return UIColorHex(0xa75f18);
case 4:
return UIColorHex(0x664412);
default:
break;
}
return nil;
}
@end