iPhoneかごめもよう

かごめ模様を描いてみようと調べていたら、「竹かごをかぶった犬張子が縁起物」というのを知ったのでなんとなくそんなiPhoneアプリを書いてみます(笑)。


動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

{

    UIView *kago;

    UIImageView *inuHariko;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    [self createInuHariko];    

    [self createKagomeMoyou];

}

– (void)createKagomeMoyou

{

    kago = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 207)];

    kago.layer.masksToBounds = YES;

    kago.backgroundColor = [UIColor clearColor];

    [self.view addSubview:kago];

    [self createSlantingLineA];

    [self createSlantingLineB];

    [self createHorizontalLine];

}

– (void)createInuHariko

{

    inuHariko = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@”inu”]];

    inuHariko.center = CGPointMake(160, 200);

    [self.view addSubview:inuHariko];

    

    inuHariko.userInteractionEnabled = YES;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];

    [inuHariko addGestureRecognizer:tap];

}

– (void)tap:(UITapGestureRecognizer*)gr

{

    for (UIView *v in self.view.subviews) {

        if ([v isKindOfClass:[UILabel class]]) {

            [v removeFromSuperview];

        }

    }

    

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

    take.text = @”;

    take.font = [UIFont systemFontOfSize:100];

    take.backgroundColor = [UIColor clearColor];

    [take sizeToFit];

    take.center = CGPointMake(250, 300);

    take.transform = CGAffineTransformMakeTranslation(0, –320);

    [self.view addSubview:take];

    

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

    inu.text = @”;

    inu.font = [UIFont systemFontOfSize:100];

    inu.backgroundColor = [UIColor clearColor];

    [inu sizeToFit];

    inu.center = CGPointMake(250, 320);

    inu.transform = CGAffineTransformMakeTranslation(-320, 0);

    [self.view addSubview:inu];

    

    [UIView animateWithDuration:0.5 animations:^{

        take.transform = CGAffineTransformIdentity;

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.5 animations:^{

            take.transform = CGAffineTransformMakeScale(1.0, 0.2);

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:0.5 animations:^{

                inu.transform = CGAffineTransformIdentity;

            } completion:^(BOOL finished) {

                [UIView animateWithDuration:0.5 animations:^{

                    inu.transform = CGAffineTransformMakeScale(1.0, 0.2);

                } completion:^(BOOL finished) {

                    // flip

                    [UIView animateWithDuration:0.5 animations:^{

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

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

                    } completion:^(BOOL finished) {

                        inu.text = @”;

                        inu.font = [UIFont systemFontOfSize:50];

                        [inu sizeToFit];

                        inu.center = CGPointMake(260, 320);

                        [UIView animateWithDuration:0.5 animations:^{

                            inu.layer.transform = CATransform3DIdentity;

                        } completion:^(BOOL finished) {

                            

                            CABasicAnimation *wara = [CABasicAnimation animationWithKeyPath:@”transform.rotation”];

                            wara.duration = 0.5;

                            wara.autoreverses = YES;

                            wara.toValue = @(M_PI * 0.2);

                            wara.repeatCount = 5;

                            [inu.layer addAnimation:wara forKey:nil];

                        }];

                    }];

                }];

            }];

        }];

    }];

}

– (void)createSlantingLineA

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    // y = √x + c

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

        float x0 = 0;

        float y0 = i * 30500;

        [path moveToPoint:CGPointMake(x0, y0)];

        

        float x1 = 320;

        float y1 = 320 * sqrt(3.0) + y0;

        [path addLineToPoint:CGPointMake(x1, y1)];

    }

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = [UIColor clearColor].CGColor;

    sl.strokeColor = [UIColor brownColor].CGColor;

    sl.lineWidth = 3;

    sl.path = path.CGPath;

    

    [kago.layer addSublayer:sl];

}

– (void)createSlantingLineB

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    // y = √x + c

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

        float x0 = 320;

        float y0 = i * 30500;

        [path moveToPoint:CGPointMake(x0, y0)];

        

        float x1 = 0;

        float y1 = 320 * sqrt(3.0) + y0;

        [path addLineToPoint:CGPointMake(x1, y1)];

    }

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = [UIColor clearColor].CGColor;

    sl.strokeColor = [UIColor brownColor].CGColor;

    sl.lineWidth = 3;

    sl.path = path.CGPath;

    

    [kago.layer addSublayer:sl];

}

– (void)createHorizontalLine

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    // y = x + c

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

        float x0 = 0;

        float y0 = i * 15 + 9;

        [path moveToPoint:CGPointMake(x0, y0)];

        

        float x1 = 320;

        float y1 = y0;

        [path addLineToPoint:CGPointMake(x1, y1)];

    }

    

    CAShapeLayer *sl = [CAShapeLayer layer];

    sl.fillColor = [UIColor clearColor].CGColor;

    sl.strokeColor = [UIColor brownColor].CGColor;

    sl.lineWidth = 3;

    sl.path = path.CGPath;

    

    [kago.layer addSublayer:sl];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end