iPhone何人?

バスに何人乗っているか数えてみよう!という感じのiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) UIView *redBus;

@property (nonatomic, weak) UIView *greenBus;

@property (nonatomic, strong) NSMutableArray *pushBehaviorArr;

@property (nonatomic, strong) UIDynamicAnimator *animator;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    [self createLabel];

    [self createBus];

    [self createPuppets];

    [self setupAnimator];

    

    UIButton *start = [UIButton buttonWithType:UIButtonTypeSystem];

    [start setTitle:@”START” forState:UIControlStateNormal];

    start.titleLabel.font = [UIFont boldSystemFontOfSize:50];

    [start sizeToFit];

    start.center = CGPointMake(160, 430);

    [self.view addSubview:start];

    

    [start addTarget:self action:@selector(startPush) forControlEvents:UIControlEventTouchUpInside];

}

– (void)createLabel

{

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

    logo.font = [UIFont systemFontOfSize:100];

    logo.textColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.5];

    logo.text = @”How many”;

    [logo sizeToFit];

    logo.center = CGPointMake(160, 250);

    logo.transform = CGAffineTransformMakeRotation(M_PI/4.0);

    [self.view addSubview:logo];

}

– (void)createBus

{

    self.redBus = [self createBus:[UIColor redColor]];

    self.redBus.center = CGPointMake(350, 100);

    self.greenBus = [self createBus:[UIColor greenColor]];

    self.greenBus.center = CGPointMake(350, 300);

}

– (UIView *)createBus:(UIColor *)color

{

    UIView *bus = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 80)];

    bus.backgroundColor = color;

    [self.view addSubview:bus];

    

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

        float w = CGRectGetMaxX(bus.bounds) / 3.3;

        float x = (i % 3) * w + 10;

        float y = (i / 3) * w*0.6 + 10;

        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(x, y, w*0.8, w*0.5)];

        v.tag = 1;

        v.backgroundColor = [UIColor whiteColor];

        [bus addSubview:v];

    }

    

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

        float x = i * 100 + 10;

        UIView *tire = [[UIView alloc] initWithFrame:CGRectMake(x, 70, 30, 30)];

        tire.layer.cornerRadius = 15;

        tire.backgroundColor = [UIColor blackColor];

        [bus addSubview:tire];

    }

    

    return bus;

}

– (void)createPuppets

{

    NSMutableArray *windows = [NSMutableArray array];

    [windows addObjectsFromArray:self.redBus.subviews];

    [windows addObjectsFromArray:self.greenBus.subviews];

    

    [windows enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        UIView *v = (UIView*)obj;

        BOOL rand = arc4random() % 2 == 1;

        if (v.tag == 1 && rand) {

            UIView *face = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 12)];

            face.backgroundColor = [UIColor blackColor];

            face.layer.cornerRadius = 6;

            face.center = CGPointMake(CGRectGetMidX(v.bounds), CGRectGetMidY(v.bounds));

            [v addSubview:face];

            

            UIView *body = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 5)];

            body.center = CGPointMake(CGRectGetMidX(v.bounds), CGRectGetMidY(v.bounds) + 10);

            body.backgroundColor = face.backgroundColor;

            [v addSubview:body];

        }

    }];

}

– (void)setupAnimator

{

    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

    

    

    self.pushBehaviorArr = [NSMutableArray array];

    for (UIView *bus in @[self.redBus, self.greenBus]) {

        UIPushBehavior *pushBehavior = [[UIPushBehavior alloc] initWithItems:@[bus] mode:UIPushBehaviorModeContinuous];

        pushBehavior.angle = 0.0;

        [animator addBehavior:pushBehavior];

        

        [self.pushBehaviorArr addObject:pushBehavior];

    }

    

    self.animator = animator;

}

– (void)startPush

{

    for (UIPushBehavior *pushBehavior in self.pushBehaviorArr) {

        int v = (arc4random() % 20) * 0.2;

        pushBehavior.magnitude = – v – 0.5;

    }

    

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

}

– (void)check:(NSTimer *)sender

{

    if (!CGRectIntersectsRect(self.view.frame, self.redBus.frame)

        && !CGRectIntersectsRect(self.view.frame, self.greenBus.frame)) {

        [sender invalidate];

        

        [self performSelector:@selector(reset) withObject:nil afterDelay:2.0];

    }

}

– (void)reset

{

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

        [obj removeFromSuperview];

    }];

    

    [self createLabel];

    [self createBus];

    [self createPuppets];

    [self setupAnimator];

    

    UIButton *start = [UIButton buttonWithType:UIButtonTypeSystem];

    [start setTitle:@”START” forState:UIControlStateNormal];

    start.titleLabel.font = [UIFont boldSystemFontOfSize:50];

    [start sizeToFit];

    start.center = CGPointMake(160, 430);

    [self.view addSubview:start];

    

    [start addTarget:self action:@selector(startPush) forControlEvents:UIControlEventTouchUpInside];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end