表示される四角を数えて、かずをタップするiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@interface ViewController ()
@property (nonatomic, strong) NSMutableDictionary *views;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.views = [NSMutableDictionary dictionary];
[self createMainView];
[self createButtons];
[self updateLayout];
[self question];
}
– (void)createMainView
{
UIView *v = [[UIView alloc] init];
v.layer.borderWidth = 4;
v.layer.borderColor = [UIColor colorWithWhite:0.9 alpha:1].CGColor;
v.layer.masksToBounds = YES;
[self.view addSubview:v];
v.translatesAutoresizingMaskIntoConstraints = NO;
[self.views setObject:v forKey:@”main”];
}
– (void)createButtons
{
for (int i=0; i<3; i++) {
UIButton *btn = [[UIButton alloc] init];
btn.translatesAutoresizingMaskIntoConstraints = NO;
btn.backgroundColor = [UIColor blueColor];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:40];
[btn setTitle:[@(i+1) stringValue] forState:UIControlStateNormal];
[self.view addSubview:btn];
[self.views setObject:btn forKey:[NSString stringWithFormat:@”btn%d”, i+1]];
[btn addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];
}
}
– (void)updateLayout
{
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”|-10-[main]-10-|” options:NSLayoutFormatAlignAllBottom metrics:nil views:self.views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:|-40-[main]-160-|” options:NSLayoutFormatAlignAllBottom metrics:nil views:self.views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”|-[btn1(80)]-20-[btn2(80)]-20-[btn3(80)]-|” options:0 metrics:nil views:self.views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:[btn1(80)]-40-|” options:0 metrics:nil views:self.views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:[btn2(80)]-40-|” options:0 metrics:nil views:self.views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:[btn3(80)]-40-|” options:0 metrics:nil views:self.views]];
}
– (void)push:(UIButton *)sender
{
[UIView animateWithDuration:0.2 animations:^{
sender.alpha = 0.3;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 animations:^{
sender.alpha = 1.0;
}];
}];
UIView *m = self.views[@”main”];
if (m.tag == [sender.titleLabel.text intValue]) {
UIView *v = [[UIView alloc] initWithFrame:CGRectInset(m.frame, 15, 15)];
v.backgroundColor = [UIColor greenColor];
[self.view addSubview:v];
UILabel *l = [[UILabel alloc] init];
l.text = @”OK”;
l.center = CGPointMake(50, 200);
l.textColor = [UIColor whiteColor];
l.font = [UIFont boldSystemFontOfSize:80];
[l sizeToFit];
[v addSubview:l];
v.transform = CGAffineTransformMakeTranslation(0, –400);
[UIView animateWithDuration:0.5 animations:^{
v.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[self question];
[UIView animateWithDuration:0.5 animations:^{
v.transform = CGAffineTransformMakeTranslation(0, –400);
} completion:^(BOOL finished) {
[v removeFromSuperview];
}];
}];
}
}
– (void)question
{
int q = (arc4random() % 3) + 1;
UIView *m = self.views[@”main”];
if (q == m.tag) {
[self question];
return;
}
m.tag = q;
[m.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[m removeConstraints:m.constraints];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (int i=0; i<q; i++) {
UIView *v = [[UIView alloc] init];
v.backgroundColor = [UIColor greenColor];
v.translatesAutoresizingMaskIntoConstraints = NO;
[m addSubview:v];
[dict setObject:v forKey:[NSString stringWithFormat:@”v%d”, i+1]];
}
// layout
switch (q) {
case 1:
[m addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”|-40-[v1]-40-|” options:0 metrics:nil views:dict]];
[m addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:|-40-[v1]-40-|” options:0 metrics:nil views:dict]];
break;
case 2:
[m addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:|-100-[v1]-100-|” options:0 metrics:nil views:dict]];
[m addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:|-100-[v2]-100-|” options:0 metrics:nil views:dict]];
[m addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”|-30-[v1(100)]” options:0 metrics:nil views:dict]];
[m addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”[v2(100)]-30-|” options:0 metrics:nil views:dict]];
break;
case 3:
[m addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:|-160-[v1(50)]” options:0 metrics:nil views:dict]];
[m addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:|-60-[v2(50)]” options:0 metrics:nil views:dict]];
[m addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”V:|-160-[v3(50)]” options:0 metrics:nil views:dict]];
[m addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@”|-50-[v1(50)]-30-[v2(50)]-30-[v3(50)]” options:0 metrics:nil views:dict]];
break;
default:
break;
}
}
@end