iPhone砂時計パラパラ

パラパラ漫画みたいな感じで砂時計というiPhoneアプリのサンプルコードを描いてみます。


今回使った画像


動画でサンプルを確認
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

@interface ViewController ()

@property (strong, nonatomic) NSMutableArray *pictures;

@property (strong, nonatomic) NSTimer *timer;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createHourglass];

    [self createButton];

}

– (void)createHourglass

{

    if (!self.pictures) self.pictures = [[NSMutableArray alloc] init];

    

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

        UIView *pict = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

        pict.backgroundColor = [UIColor whiteColor];

        [self.view addSubview:pict];

        pict.layer.anchorPoint = CGPointMake(0.5, 1.01);

        pict.layer.position = CGPointMake(160, 200);

        pict.layer.cornerRadius = 5;

        pict.layer.masksToBounds = YES;

        [self.pictures addObject:pict];

        

        UIView *sand = [[UIView alloc] initWithFrame:CGRectMake(0, 1005*i, 100, 5*i)];

        sand.tag = 1;

        sand.backgroundColor = [UIColor blackColor];

        [pict addSubview:sand];

        

        UIImage *image = [UIImage imageNamed:@”glass.png”];

        UIImageView *iv = [[UIImageView alloc] initWithImage:image];

        iv.frame = pict.bounds;

        [pict addSubview:iv];

        

    }

}

– (void)createButton

{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setTitle:@” start “ forState:UIControlStateNormal];

    btn.titleLabel.font = [UIFont systemFontOfSize:30];

    btn.backgroundColor = [UIColor blackColor];

    [btn sizeToFit];

    btn.center = CGPointMake(160, 400);

    btn.layer.cornerRadius = 5;

    [self.view addSubview:btn];

    

    [btn addTarget:self action:@selector(push) forControlEvents:UIControlEventTouchUpInside];

}

– (void)push

{

    if (!self.timer) {

        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick:) userInfo:Nil repeats:YES];

    }

}

– (void)tick:(NSTimer*)sender

{

    if ([self.pictures count]) {

        UIView *v = [self.pictures lastObject];

        

        [self.view addSubview:v];

        [UIView animateWithDuration:0.3 animations:^{

            v.layer.transform = CATransform3DMakeRotation(-M_PI/2.0, 1, 0, 0);

        } completion:^(BOOL finished) {

            

            UIView *sand = [v viewWithTag:1];

            sand.frame = CGRectMake(0, 0, 100, 100 – sand.frame.size.height);

            [UIView animateWithDuration:0.3 animations:^{

                v.layer.transform = CATransform3DRotate(v.layer.transform, –M_PI/2.0, 1, 0, 0);

            }];

        }];

        

        [self.pictures removeLastObject];

    } else {

        [self.timer invalidate];

        self.timer = nil;

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end