iPhoneブラインド

窓にブラインド的なiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic) CGPoint begin;

@property (nonatomic) BOOL open;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:1.0 alpha:1];

    

    [self createTitle];

    [self createBlinds];

    [self createOpenButton];

}

– (void)createTitle

{

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

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

        title.text = @”BLINDS”;

        title.font = [UIFont fontWithName:@”Chalkduster” size:60];

        title.textColor = [UIColor lightGrayColor];

        [title sizeToFit];

        title.center = CGPointMake(160, i * 140);

        title.transform = CGAffineTransformMakeRotation(M_PI * 0.2);

        [self.view addSubview:title];

    }

}

#define BlindTag 1

– (void)createBlinds

{

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

        float x = 160;

        float y = i * 30;

        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];

        v.center = CGPointMake(x, y);

        v.tag = BlindTag;

        v.backgroundColor = [UIColor brownColor];

        v.layer.shadowColor = [UIColor whiteColor].CGColor;

        v.layer.shadowRadius = 1;

        v.layer.shadowOpacity = 0.5;

        v.layer.zPosition = 100;

        [self.view addSubview:v];

    }

}

– (void)createOpenButton

{

    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];

    b.frame = CGRectMake(260, CGRectGetMaxY(self.view.frame) – 70, 50, 60);

    b.titleLabel.font = [UIFont systemFontOfSize:20];

    [b setTitle:@”↑↓” forState:UIControlStateNormal];

    b.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];

    b.layer.zPosition = 200;

    b.layer.cornerRadius = 5;

    [self.view addSubview:b];

    

    [b addTarget:self action:@selector(openBlind) forControlEvents:UIControlEventTouchUpInside];

}

– (void)openBlind

{

    NSPredicate *pred = [NSPredicate predicateWithFormat:@”tag == %d”, 1];

    NSArray *blinds = [self.view.subviews filteredArrayUsingPredicate:pred];

    

    [UIView animateWithDuration:0.1 animations:^{

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

            UIView *blind = blinds[i];

            blind.layer.transform = self.open ? CATransform3DIdentity : CATransform3DMakeRotation(M_PI * 0.4, 1, 0, 0);

        }

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:1.5 animations:^{

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

                UIView *blind = blinds[i];

                blind.center = self.open ? CGPointMake(160, i*30): ((UIView *)blinds[0]).center;

            }

        } completion:^(BOOL finished) {

            

            self.open = !self.open;

        }];

    }];

    

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    self.begin = [[touches anyObject] locationInView:self.view];

}

– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    CGPoint p = [[touches anyObject] locationInView:self.view];

    float dx = p.xself.begin.x;

    

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

        if (v.tag == BlindTag) {

            v.layer.transform = CATransform3DMakeRotation(dx / 160.0, 1, 0, 0);

        }

    }];

}

@end