パネルをタップするだけの簡単なクイズアプリを作ってみます。足し算、引き算、アルファベットの虫食い問題が出題されます、正解の選択肢パネルを全てタップできたらクリアです。
動作イメージ
XcodeからiOS6 iPhone Simulatorで動かすとこんな感じになります。
ポイント
問題一つにつき、5個の選択肢をパネルで表示します。一問一問で正解を判定するのではなく、正解のパネルを全てタップ出来ているかどうかという判定にしました。tagに1 ~ 25の数字をいれて、この数値から、どの問題の何番目の選択肢かを算出しています。
サンプルコード
#import “ViewController.h”
#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]
@interface ViewController () {
int selected[5];
int answers[5];
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [self color:4];
[self createQuestions];
}
– (void)createQuestions
{
// question:choices(answer, choice1, choice2, choice3…)
NSDictionary *questionAndChoices =
@{
@”A B C ? E”: @[@4, @”A”,@”B”,@”C”,@”D”,@”E”],
@”1 + 2 = ?”: @[@3, @”1″,@”2″,@”3″,@”4″,@”5″],
@”2 + ? = 6″: @[@4, @”6″,@”8″,@”3″,@”4″,@”5″],
@”Z Y X ? V”: @[@5, @”O”,@”Q”,@”S”,@”U”,@”W”],
@”3 – 1 = ?”: @[@2, @”3″,@”2″,@”6″,@”4″,@”5″],
};
int i=0;
float h = 90;
for (NSString *key in questionAndChoices) {
float y = h * i;
UILabel *question = [[UILabel alloc] initWithFrame:CGRectMake(15, y + 5, 260, h – h / 1.618)];
question.text = key;
question.font = [UIFont systemFontOfSize:15];
question.backgroundColor = [UIColor clearColor];
[self.view addSubview:question];
[self createChoices:[questionAndChoices valueForKey:key] row:i];
i++;
}
}
– (void)createChoices:(NSArray*)choices row:(int)row
{
float h = 90;
// 0:answer
answers[row] = [[choices objectAtIndex:0] intValue];
for (int i = 1; i<[choices count]; i++) {
float x = (i – 1) * (h / 1.618 + 5) + 10;
float y = row * h + h * (1 – 1/1.618);
UILabel *choice = [[UILabel alloc] initWithFrame:CGRectMake(x, y, h / 1.618, h / 1.618)];
choice.backgroundColor = [self color:2];
choice.text = [choices objectAtIndex:i];
choice.textAlignment = 1;
choice.textColor = [self color:3];
choice.font = [UIFont boldSystemFontOfSize:40];
choice.tag = row * 5 + i;
[self.view addSubview:choice];
choice.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[choice addGestureRecognizer:tap];
}
}
– (void)tap:(UITapGestureRecognizer*)gr
{
int no = gr.view.tag; // 1 ~ 25.
int questionNo = (no – 1) / 5; // 0 ~ 4
int selectedNo = (no – 1) % gr.view.tag % 5; // 0 ~ 4
if (selected[questionNo] != 0) {
int currentNo = questionNo * 5 + selected[questionNo];
UIView *current = [self.view viewWithTag:currentNo];
current.backgroundColor = [self color:2];
}
selected[questionNo] = selectedNo + 1; // 1 ~ 5
gr.view.backgroundColor = [self color:0];
// check clear
[self checkAnswer];
}
– (void)checkAnswer
{
BOOL correct = YES;
for (int i=0; i<5; i++) {
if(selected[i] != answers[i]) {
correct = NO;
}
}
if (correct) {
UILabel *clear = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, 320, 100)];
clear.font = [UIFont boldSystemFontOfSize:40];
clear.text = @”clear”;
clear.textAlignment = 1;
clear.textColor = [self color:3];
clear.backgroundColor = [self color:1];
clear.transform = CGAffineTransformMakeTranslation(0, –300);
[self.view addSubview:clear];
[UIView animateWithDuration:0.5 animations:^{
clear.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[self performSelector:@selector(restart:) withObject:clear afterDelay:1.0];
}];
}
}
– (void)restart:(UILabel*)label
{
[label removeFromSuperview];
for (int i=0; i<5; i++) {
selected[i] = 0;
}
for (UIView *v in self.view.subviews) {
[v removeFromSuperview];
}
[self createQuestions];
}
– (UIColor*)color:(int)i
{
switch (i) {
case 0:
return UIColorHex(0xDE5605);
case 1:
return UIColorHex(0xF7A035);
case 2:
return UIColorHex(0xB1DE85);
case 3:
return UIColorHex(0xEFECCA);
case 4:
return UIColorHex(0x65ABA6);
default:
break;
}
return nil;
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end