サメがパクパクするiPhoneアプリのサンプルコードを描いてみます。
画像パーツ
#import “ViewController.h”
@interface ViewController ()
@property (nonatomic, weak) UIView *sharkHead;
@property (nonatomic, strong) NSMutableArray *fishBox;
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.view.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:1.0 alpha:1];
[self createShark];
[self createButton];
[self start];
}
– (void)createShark
{
UIImageView *body = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”body”]];
body.frame = CGRectMake(0, 0, body.bounds.size.width * 0.2, body.bounds.size.height * 0.2);
body.center = CGPointMake(100, 150);
[self.view addSubview:body];
UIImageView *head = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”head”]];
head.frame = CGRectMake(0, 0, head.bounds.size.width * 0.2, head.bounds.size.height * 0.2);
head.layer.anchorPoint = CGPointMake(0.2, 0.8);
head.layer.position = CGPointMake(130, 170);
[self.view addSubview:head];
self.sharkHead = head;
}
– (void)createButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 80, 80);
[button setBackgroundColor:[UIColor redColor]];
button.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds) – 50);
button.layer.cornerRadius = 40;
[button setTitle:@”B” forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:40];
[self.view addSubview:button];
[button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
}
– (void)tap:(UIButton *)sender
{
[UIView animateWithDuration:0.4 animations:^{
self.sharkHead.transform = CGAffineTransformMakeRotation(M_PI * 0.2);
sender.transform = CGAffineTransformMakeRotation(M_PI * 0.1);
} completion:^(BOOL finished) {
sender.transform = CGAffineTransformIdentity;
[self.fishBox enumerateObjectsUsingBlock:^(UIView *fish, NSUInteger idx, BOOL *stop) {
if (CGRectIntersectsRect(fish.frame, CGRectMake(160, 150, 40, 40))) {
[UIView animateWithDuration:0.3 animations:^{
fish.alpha = 0.1;
fish.transform = CGAffineTransformMakeScale(0.2, 0.2);
} completion:^(BOOL finished) {
[fish removeFromSuperview];
}];
}
}];
[UIView animateWithDuration:0.4 animations:^{
self.sharkHead.transform = CGAffineTransformIdentity;
}];
}];
}
– (UIView *)createFish
{
UIView *fish = [[UIView alloc] init];
fish.center = CGPointMake(CGRectGetMaxX(self.view.bounds) – 80, 160);
[self.view addSubview:fish];
CALayer *head = [CALayer layer];
head.frame = CGRectMake(0, 0, 40, 40);
head.transform = CATransform3DMakeRotation(M_PI * 0.25, 0, 0, 1);
head.backgroundColor = [UIColor yellowColor].CGColor;
[fish.layer addSublayer:head];
UIBezierPath *tri = [UIBezierPath bezierPath];
[tri moveToPoint:CGPointMake(0, 0)];
[tri addLineToPoint:CGPointMake(15, –15)];
[tri addLineToPoint:CGPointMake(15, 15)];
[tri closePath];
CAShapeLayer *tail = [CAShapeLayer layer];
tail.path = tri.CGPath;
tail.position = CGPointMake(40, 20);
tail.fillColor = [UIColor yellowColor].CGColor;
[fish.layer addSublayer:tail];
UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:3 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
CAShapeLayer *eye = [CAShapeLayer layer];
eye.path = circlePath.CGPath;
eye.position = CGPointMake(5, 15);
[fish.layer addSublayer:eye];
if (!self.fishBox) {
self.fishBox = [NSMutableArray array];
}
[self.fishBox addObject:fish];
return fish;
}
– (void)start
{
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
– (void)tick:(NSTimer *)sender
{
static int counter;
counter++;
if (counter > 30) {
counter = 0;
[self createFish];
}
[self.fishBox enumerateObjectsUsingBlock:^(UIView *fish, NSUInteger idx, BOOL *stop) {
fish.center = CGPointMake(fish.center.x – 5, fish.center.y);
if (fish.center.x < –50) {
[fish removeFromSuperview];
}
}];
}
@end