iPhone細切り数字

数字を細切りにしてびよーんとするiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    

    [self createButton];

}

– (void)createButton

{

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

        UIButton *number = [UIButton buttonWithType:UIButtonTypeCustom];

        [number setTitle:[@(i+1) stringValue] forState:UIControlStateNormal];

        number.titleLabel.font = [UIFont boldSystemFontOfSize:120];

        [number sizeToFit];

        number.center = CGPointMake(50, i * 120 + 80);

        [self.view addSubview:number];

        

        [number addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];

    }

}

– (void)tap:(UIButton *)sender

{

    NSArray *arr = [self sliceView:sender];

    for (int i=0; i<arr.count; i++) {

        UIView *v = arr[i];

        v.center = CGPointMake(200, 6 * i + 30);

        [UIView animateWithDuration:1.0 animations:^{

            v.center = CGPointMake(200, 15 * i + 100);

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:1.0 animations:^{

                v.center = CGPointMake(200, 6 * i + 102);

            } completion:^(BOOL finished) {

                [UIView animateWithDuration:1.0 animations:^{

                    v.center = CGPointMake(200, 6 * i + 600);

                } completion:^(BOOL finished) {

                    [v removeFromSuperview];

                }];

            }];

        }];

    }

}

– (NSArray *)sliceView:(UIView *)v

{

    NSMutableArray *arr = [NSMutableArray array];

    

    int count = 30;

    float dh = CGRectGetMaxY(v.bounds) / count;

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

        UIGraphicsBeginImageContext(CGSizeMake(150, dh));

        CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 50, -dh * i);

        

        [v.layer renderInContext:UIGraphicsGetCurrentContext()];

        

        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        

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

        iv.backgroundColor = [UIColor redColor];

        [self.view addSubview:iv];

        

        [arr addObject:iv];

    }

    

    return arr;

}

@end