标签:
------------- ViewController.m -------------
#import "ViewController.h"
#import "CZQuestion.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *countView;
@property (weak, nonatomic) IBOutlet UILabel *titleView;
@property (weak, nonatomic) IBOutlet UIButton *coinView;
@property (weak, nonatomic) IBOutlet UIButton *iconView;
@property (weak, nonatomic) IBOutlet UIButton *nextView;
@property (nonatomic, weak) UIButton *coverView;
@property (nonatomic, strong) NSArray *questions;
@property (weak, nonatomic) IBOutlet UIView *answersView;
@property (weak, nonatomic) IBOutlet UIView *optionsView;
@property (nonatomic, assign) int index;
- (IBAction)tipClick;
- (IBAction)bigImageClick;
- (IBAction)nextClick;
- (IBAction)iconViewClick;
@end
@implementation ViewController
- (NSArray *)questions
{
if (_questions == nil)
{
_questions = [CZQuestion questionsList];
}
return _questions;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.index--;
[self nextClick];
}
- (IBAction)bigImageClick
{
CGFloat iconW = self.view.frame.size.width;
CGFloat iconH = iconW;
CGFloat iconX = 0;
CGFloat iconY = (self.view.frame.size.height - iconW) / 2;
UIButton *coverView = [[UIButton alloc] init];
[self.view addSubview:coverView];
coverView.frame = self.view.bounds;
coverView.backgroundColor = [UIColor blackColor];
coverView.alpha = 0;
[self.view bringSubviewToFront:self.iconView];
[UIView animateWithDuration:1.0 animations:^{
self.iconView.frame = CGRectMake(iconX, iconY, iconW, iconH);
coverView.alpha = 0.5;
}];
[coverView addTarget:self action:@selector(smallImgClick) forControlEvents:UIControlEventTouchUpInside];
self.coverView = coverView;
}
- (void)smallImgClick
{
[UIView animateWithDuration:1.0 animations:^{
self.iconView.frame = CGRectMake(80, 111, 160, 160);
self.coverView.alpha = 0;
} completion:^(BOOL finished) {
[self.coverView removeFromSuperview];
self.coverView = nil;
}];
}
- (IBAction)iconViewClick {
if (self.coverView == nil)
{
[self bigImageClick];
}else
{
[self smallImgClick];
}
}
- (IBAction)nextClick
{
self.index ++;
if (self.index == self.questions.count)
{
UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"提示" message:@"恭喜过关" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[view show];
return;
}
CZQuestion *question = self.questions[self.index];
self.titleView.text = question.title;
[self.iconView setImage:[UIImage imageNamed:question.icon] forState:UIControlStateNormal];
self.countView.text = [NSString stringWithFormat:@"%d/%lu",self.index + 1,(unsigned long)self.questions.count];
self.nextView.enabled = self.index != self.questions.count - 1;
[self setAnswerButtons:question];
[self setOptionButtons:question];
}
- (void)setAnswerButtons:(CZQuestion *)question
{
[self.answersView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
NSUInteger answerCount = question.answer.length;
CGFloat margin = 10;
CGFloat btnW = 35;
CGFloat btnH = 35;
CGFloat leftMargin = (self.answersView.frame.size.width - answerCount * btnW - (answerCount - 1) * margin) / 2;
CGFloat btnY = 0;
for (int i = 0; i < answerCount; i++)
{
UIButton *answerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.answersView addSubview:answerBtn];
CGFloat btnX = leftMargin + i * (btnW + margin);
answerBtn.frame = CGRectMake(btnX, btnY, btnW, btnH);
[answerBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
[answerBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
[answerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[answerBtn addTarget:self action:@selector(setAnswerToOption:) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)setOptionButtons:(CZQuestion *)question
{
self.optionsView.userInteractionEnabled = YES;
[self.optionsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
CGFloat btnW = 35;
CGFloat btnH = 35;
int columnCount = 7;
CGFloat marginX = (self.optionsView.frame.size.width - columnCount * btnW) / (columnCount + 1);
CGFloat marginY = 10;
for (int i = 0; i < question.options.count; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.optionsView addSubview:btn];
int row = i / columnCount;
int column = i % columnCount;
CGFloat btnX = marginX + column * (btnW + marginX);
CGFloat btnY = row * (btnH + marginY);
btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
[btn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitle:question.options[i] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(setOptionToAnswer:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = i;
}
}
- (void)setOptionToAnswer:(UIButton *)sender
{
sender.hidden = YES;
for (UIButton *answerBtn in self.answersView.subviews)
{
if (answerBtn.currentTitle == nil)
{
[answerBtn setTitle:sender.currentTitle forState:UIControlStateNormal];
answerBtn.tag = sender.tag;
break;
}
}
BOOL isFull = YES;
NSMutableString *inputAnswer = [NSMutableString string];
for (UIButton *answerBtn in self.answersView.subviews)
{
if (answerBtn.currentTitle == nil)
{
isFull = NO;
break;
}
[inputAnswer appendString:answerBtn.currentTitle];
}
CZQuestion *question = self.questions[self.index];
if (isFull)
{
self.optionsView.userInteractionEnabled = NO;
if ([question.answer isEqualToString:inputAnswer])
{
[self setAnswerBtnColor:[UIColor blueColor]];
int coin = [self.coinView.currentTitle intValue];
coin += 500;
[self.coinView setTitle:[NSString stringWithFormat:@"%d",coin] forState:UIControlStateNormal];
[self performSelector:@selector(nextClick) withObject:nil afterDelay:1.0];
}else
{
[self setAnswerBtnColor:[UIColor redColor]];
}
}
}
- (void)setAnswerToOption:(UIButton *)sender
{
self.optionsView.userInteractionEnabled = YES;
for (UIButton *optionBtn in self.optionsView.subviews)
{
if (sender.tag == optionBtn.tag)
{
optionBtn.hidden = NO;
break;
}
}
[sender setTitle:nil forState:UIControlStateNormal];
[self setAnswerBtnColor:[UIColor blackColor]];
}
- (void)setAnswerBtnColor:(UIColor *)color
{
for (UIButton *answerBtn in self.answersView.subviews)
{
[answerBtn setTitleColor:color forState:UIControlStateNormal];
}
}
- (IBAction)tipClick
{
self.index--;
[self nextClick];
CZQuestion *question = self.questions[self.index];
NSString *first = [NSString stringWithFormat:@"%C",[question.answer characterAtIndex:0]];
for (UIButton *btn in self.optionsView.subviews)
{
NSString *option = btn.currentTitle;
if ([first isEqualToString:option])
{
[self setOptionToAnswer:btn];
}
}
int coin = [self.coinView.currentTitle intValue];
coin -= 1000;
[self.coinView setTitle:[NSString stringWithFormat:@"%d",coin] forState:UIControlStateNormal];
}
@end
标签:
原文地址:http://www.cnblogs.com/lixiang2015/p/4708902.html