iPhone黒のなにか

のびてくる黒いなにかをタップするiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    [self createHoles];

    [self start];

}

– (void)createHoles

{

    CGPoint o = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame) + 50);

    float r = 100;

    for (int i=0; i<8; i++) {

        float x = r * cos(M_PI_4 * i) + o.x;

        float y = 1.5 * r * sin(M_PI_4 * i) + o.y;

        UIView *hole = [self createHole:CGPointMake(x, y)];

        hole.tag = i+1;

    }

}

– (UIView *)createHole:(CGPoint)p

{

    UILabel *hole = [[UILabel alloc] init];

    hole.font = [UIFont fontWithName:@”AlNile-Bold” size:40];

    hole.text = @”( ● )”;

    [hole sizeToFit];

    hole.center = p;

    [self.view addSubview:hole];

    

    return hole;

}

– (void)start

{

    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(big) userInfo:nil repeats:YES];

}

– (void)big

{

    int tag = (arc4random() % 16) + 1;

    if (tag > 8) {

        return;

    }

    

    UIView *v = [self.view viewWithTag:tag];

    

    UIView *body = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 1)];

    body.backgroundColor = [UIColor blackColor];

    body.center = CGPointMake(v.center.x, v.center.y5);

    body.layer.anchorPoint = CGPointMake(0.5, 1.0);

    body.userInteractionEnabled = NO;

    [self.view addSubview:body];

    

    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

    head.tag = 100;

    head.backgroundColor = [UIColor blackColor];

    head.layer.cornerRadius = 10;

    head.center = CGPointMake(v.center.x, v.center.y5);

    head.userInteractionEnabled = NO;

    [self.view addSubview:head];

    

    for (int i=0; i<2; i++) {

        UIView *eye = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 4, 4)];

        eye.center = CGPointMake(5 + i* 10, 10);

        eye.backgroundColor = [UIColor whiteColor];

        [head addSubview:eye];

    }

    [UIView animateWithDuration:2.0 animations:^{

        head.transform = CGAffineTransformMakeTranslation(0, –100);

        body.transform = CGAffineTransformMakeScale(1.0, 100);

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:2.0 animations:^{

            head.transform = CGAffineTransformIdentity;

            body.transform = CGAffineTransformIdentity;

        } completion:^(BOOL finished) {

            [head removeFromSuperview];

            [body removeFromSuperview];

        }];

    }];

    

}

– (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    

    [self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        if ([obj tag] == 100) {

            UIView *head = (UIView *)obj;

            CGPoint point = [[touches anyObject] locationInView:self.view];

            CALayer *animationLayer = [head.layer presentationLayer];

            if (CGRectContainsPoint(animationLayer.frame, point)) {

                [head.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

                    [obj removeFromSuperview];

                }];

                for (int i=0; i<2; i++) {

                    UILabel *eye = [[UILabel alloc] init];

                    eye.text = @”x”;

                    eye.textColor = [UIColor whiteColor];

                    [eye sizeToFit];

                    eye.center = CGPointMake(5 + i* 10, 10);

                    [head addSubview:eye];

                }

            }

        }

    }];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}

@end