iPhoneじどうとびら

自動で扉が閉まる的なiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@interface ViewController ()

@property (nonatomic, weak) UISwitch *autoSwitch;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    l.text = @”auto close”;

    [l sizeToFit];

    l.center = CGPointMake(self.view.center.x, self.view.center.y40);

    [self.view addSubview:l];

    

    UISwitch *sw = [[UISwitch alloc] init];

    sw.center = self.view.center;

    sw.on = YES;

    [self.view addSubview:sw];

    [sw addTarget:self action:@selector(changeSW:) forControlEvents:UIControlEventValueChanged];

    self.autoSwitch = sw;

    

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

        float x = 120 * cos(M_PI_4 * i) + self.view.center.x30;

        float y = 160 * sin(M_PI_4 * i) + self.view.center.y;

        UIView *door = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];

        door.tag = 1;

        door.layer.cornerRadius = 30;

        door.backgroundColor = [UIColor brownColor];

        door.center = CGPointMake(x, y);

        door.layer.anchorPoint = CGPointMake(0.05, 0.5);

        [self.view addSubview:door];

        

        UILabel *hole = [[UILabel alloc] initWithFrame:door.frame];

        hole.text = [@(i+1) stringValue];

        hole.textColor = [UIColor whiteColor];

        hole.font = [UIFont fontWithName:@”chalkduster” size:30];

        hole.textAlignment = NSTextAlignmentCenter;

        hole.layer.masksToBounds = YES;

        hole.backgroundColor = [UIColor blackColor];

        hole.layer.cornerRadius = 30;

        [self.view insertSubview:hole belowSubview:door];

        

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

        [door addGestureRecognizer:tap];

    }

}

– (void)openDoor:(UITapGestureRecognizer *)gr

{

    [UIView animateWithDuration:0.5 animations:^{

        gr.view.transform = CGAffineTransformMakeRotation(M_PI*1.3);

    }];

    

    if (self.autoSwitch.isOn) {

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [UIView animateWithDuration:0.5 animations:^{

                gr.view.transform = CGAffineTransformIdentity;

            }];

        });

    }

}

– (void)changeSW:(UISwitch *)sender

{

    if (sender.isOn) {

        NSPredicate *pred = [NSPredicate predicateWithFormat:@”tag=1″];

        [[self.view.subviews filteredArrayUsingPredicate:pred] enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {

            if (!CGAffineTransformIsIdentity(obj.transform)) {

                [UIView animateWithDuration:0.5 animations:^{

                    obj.transform = CGAffineTransformIdentity;

                }];

            }

        }];

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end