Wasonの四枚カード問題を試してみよう、といったiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *card;
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
self.view.backgroundColor = [self color:4];
[self createTitle];
[self createCards];
[self createConsole];
}
– (void)createTitle
{
UILabel *title = [[UILabel alloc] init];
title.text = @”Wason selection task”;
title.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
[title sizeToFit];
title.center = CGPointMake(CGRectGetMidX(self.view.bounds), 40);
title.textColor = [UIColor whiteColor];
[self.view addSubview:title];
}
– (void)createCards
{
self.card = [@[@[@”A”, @”2″], @[@”D”, @”1″], @[@”4″, @”i”], @[@”7″, @”W”]] mutableCopy];
float w = CGRectGetMaxX(self.view.bounds) / 5.0;
for (int i=0; i<4; i++) {
float x = (i+1) * w;
float y = CGRectGetMidY(self.view.bounds);
UIView *card = [self createCard:self.card[i][0] w:w];
card.backgroundColor = [self color:i];
card.center = CGPointMake(x, y);
card.tag = i;
[self.view addSubview:card];
}
}
– (void)createConsole
{
UIView *console = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(self.view.bounds) * 0.7, 100)];
console.backgroundColor = [self.view.backgroundColor colorWithAlphaComponent:0.5];
console.layer.borderWidth = 5;
console.layer.borderColor = [UIColor whiteColor].CGColor;
console.layer.cornerRadius = 15;
console.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMaxY(self.view.bounds) – 10);
[self.view addSubview:console];
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, CGRectGetMaxX(console.bounds) – 40, 0)];
l.lineBreakMode = NSLineBreakByWordWrapping;
l.numberOfLines = 0;
l.text = @”If a card has a vowel on one side, then it has an even number on the other side.”;
l.textColor = [UIColor whiteColor];
[l sizeToFit];
[console addSubview:l];
}
– (UIView *)createCard:(NSString *)s w:(float)w
{
UIView *card = [[UIView alloc] initWithFrame:CGRectMake(0, 0, (w * 0.8), (w * 0.8) *1.618)];
[self.view addSubview:card];
UILabel *l = [[UILabel alloc] init];
l.text = s;
l.font = [UIFont boldSystemFontOfSize:50];
l.textColor = [UIColor whiteColor];
l.backgroundColor = [UIColor clearColor];
[l sizeToFit];
l.center = [card convertPoint:card.center fromView:self.view];
[card addSubview:l];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(flip:)];
[card addGestureRecognizer:tap];
return card;
}
– (void)flip:(UITapGestureRecognizer *)gr
{
UIView *flipCard = [self createCard:self.card[gr.view.tag][1] w:CGRectGetMaxX(gr.view.bounds)/0.8];
flipCard.tag = gr.view.tag;
// exchange data
self.card[gr.view.tag] = @[self.card[gr.view.tag][1], self.card[gr.view.tag][0]];
flipCard.backgroundColor = gr.view.backgroundColor;
[UIView transitionWithView:gr.view duration:1.0 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
[gr.view addSubview:flipCard];
} completion:^(BOOL finished) {
flipCard.center = gr.view.center;
[self.view addSubview:flipCard];
if (gr.view.tag == 3) {
CGPoint p = CGPointMake(gr.view.center.x, gr.view.center.y + 30);
[self showNG:p];
}
[gr.view removeFromSuperview];
}];
}
– (void)showNG:(CGPoint)p
{
UILabel *ng = [[UILabel alloc] init];
ng.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
ng.font = [UIFont systemFontOfSize:40];
ng.text = @”NG”;
ng.textColor = [[UIColor redColor] colorWithAlphaComponent:0.8];
ng.center = CGPointMake(600, p.y);
[ng sizeToFit];
[self.view addSubview:ng];
[UIView animateWithDuration:0.5 animations:^{
ng.center = p;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.5 animations:^{
ng.alpha = 0;
} completion:^(BOOL finished) {
[ng removeFromSuperview];
}];
}];
}
#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
– (UIColor*)color:(NSInteger)i
{
switch (i) {
case 0:
return UIColorHex(0xA4C400);
case 1:
return UIColorHex(0x1BA1E2);
case 2:
return UIColorHex(0xF472D0);
case 3:
return UIColorHex(0xFA6800);
case 4:
return UIColorHex(0x6D8764);
default:
break;
}
return nil;
}
@end