ゆびでボトルのキャップをあけると、
中身がシュワっーーと飛び出すよ!
という感じで、子供向けiPhoneアプリのサンプルコードを書いてみた。

ポイント
キャップ部分は、UITableViewを横に向けたものを
そのまま使ってます。
willDisplayCellのなかで、表示されたcellのindexを使って
キャップの高さ、中身の飛び出しを制御してみました。

環境
このiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています

pet bottle iPhone game sample code

サンプルコード


#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    [self createBottle];

    

    [self createCap];

}

– (void)createBottle

{

    

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

        UIView *bottleTop = [[UIView alloc] initWithFrame:CGRectMake(125, 170 + i * 6, 50, 5)];

        bottleTop.backgroundColor = [UIColor orangeColor];

        bottleTop.layer.cornerRadius = 3;

        [self.view addSubview:bottleTop];

    }

    

    

    

    UIView *bottle = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 100, 80)];

    bottle.backgroundColor = [UIColor orangeColor];

    bottle.layer.cornerRadius = 10.0;

    [self.view addSubview:bottle];

    

    UIView *bottleBottom = [[UIView alloc] initWithFrame:CGRectMake(100, 282, 100, 130)];

    bottleBottom.layer.cornerRadius = 10;

    bottleBottom.backgroundColor = [UIColor orangeColor];

    [self.view addSubview:bottleBottom];

}

– (void)createCap

{

    UITableView *cap = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 48, 60) style:UITableViewStylePlain];

    cap.center = CGPointMake(150, 175);

    cap.delegate = self;

    cap.dataSource = self;

    cap.backgroundColor = [UIColor whiteColor];

    cap.separatorColor = [UIColor darkGrayColor];

    cap.transform = CGAffineTransformMakeRotation(M_PI/2.0);

    [self.view addSubview:cap];

}

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 50;

}

– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 10;

}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @”Cell”;

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    return cell;

}

– (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{

    static int lastNumber;

    float moveLength;

    if (lastNumber < indexPath.row) {

        moveLength = –0.2;

    } else {

        moveLength = 0.2;

    }

    lastNumber = indexPath.row;

    

    tableView.transform = CGAffineTransformTranslate(tableView.transform, moveLength, 0);

    

    if (indexPath.row > 48) {

        [UIView animateWithDuration:0.5 animations:^{

            tableView.center = CGPointMake(160, –50);

            [self startExplosion];

        }];

    }

}

– (void)startExplosion

{

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

        

        float direction = arc4random() % 200;

        

        UIView *waterdrop = [[UIView alloc] initWithFrame:CGRectMake(130 + direction /10.0, 150, 20, 20)];

        waterdrop.layer.cornerRadius = 10;

        

        float white = arc4random() % 170;

        waterdrop.backgroundColor = [UIColor colorWithRed:white/200.0 green:white/200.0 blue:1.0 alpha:0.7];

 

        [self.view addSubview:waterdrop];

        [UIView animateWithDuration:0.5 delay:0.01 * i options:UIViewAnimationOptionCurveEaseIn animations:^{

            waterdrop.transform = CGAffineTransformMakeTranslation(direction – 100, –200);

        } completion:^(BOOL finished) {

        }];

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end