#import "ViewController.h"
#import "QuestionModel.h"
@interface ViewController ()
/**
* 下一题
*/
- (IBAction)next;
/**
* 索引值
*/
@property(nonatomic,assign)int index;
/**
* 记录数据
*/
@property(nonatomic,strong)NSArray *questions;
/**
* 索引值
*/
@property (weak, nonatomic) IBOutlet UILabel *noLabel;
/**
* 描述信息
*/
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
/**
* 头像
*/
@property (weak, nonatomic) IBOutlet UIButton *head;
/**
* 下一题
*/
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
- (IBAction)big;
/**
* 用来记录阴影
*/
@property(nonatomic,weak)UIButton *cover;
/**
* 头像按钮点击
*/
- (IBAction)headClick;
/**
* 放置答案按钮的视图
*/
@property (weak, nonatomic) IBOutlet UIView *answerView;
/**
* 放置待选项按钮的视图
*/
@property (weak, nonatomic) IBOutlet UIView *optionView;
@property (weak, nonatomic) IBOutlet UIButton *scoreBtn;
- (IBAction)tipBtnClick;
@end
@implementation ViewController
/**
* 懒加载
*/
- (NSArray *)questions{
if (_questions == nil) {
// 1.获取全路径
NSString *path =[[NSBundle mainBundle]pathForResource:@"questions.plist" ofType:nil];
// 2.读取plist文件
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 3.字典转模型
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
QuestionModel *questionModel = [QuestionModel questionWithDict:dict];
[tempArray addObject:questionModel];
}
_questions = tempArray;
}
return _questions;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.index = -1;
[self next];
}
/**
* 提示功能的实现
*/
- (IBAction)tipBtnClick {
// 1.让答案按钮中的文字消失,并且让对应的待选项按钮显示
for (UIButton *answetBtn in self.answerView.subviews) {
[self answerBtn:answetBtn];
}
// 2.让正确答案的第一个文字显示到第一个答案按钮中,并且让对应的待选项按钮隐藏想当与电机了待选项按钮)
// 2.1取出模型
QuestionModel *questionModel = self.questions[self.index];
// 2.2取出正确答案的第一文字
NSString *answer = questionModel.answer;
NSString *firstTitle = [answer substringToIndex:1];
// 2.3相当于点击了待选项按钮
for (UIButton *optionBtn in self.optionView.subviews) {
NSString *optionBtnTitle = [optionBtn titleForState:UIControlStateNormal];
if ([optionBtnTitle isEqualToString:firstTitle]) {//表示找到了对应的按钮
[self optionBtnClick:optionBtn];
}
}
// 3.扣分
[self changeScore:-5000];
}
//下一题功能
- (IBAction)next {
if (self.index == 9) {
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"恭喜你,过关了" preferredStyle:UIAlertControllerStyleActionSheet];
[self presentViewController:alertVc animated:NO completion:nil];
[ alertVc addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
return;
}
// 1.索引值加一
self.index ++;
// 2.根据索引值设置数据
QuestionModel *quesitonModel = self.questions[self.index];
self.noLabel.text = [NSString stringWithFormat:@"%d/%ld",self.index+1,self.questions.count];
self.descLabel.text = quesitonModel.title;
[self.head setImage:[UIImage imageNamed: quesitonModel.icon] forState:UIControlStateNormal];
self.nextBtn.enabled = (self.index != self.questions.count - 1);
// 3.添加答案按钮
// 3.0添加按钮之前移除上一次按钮
for (UIButton *answerBtn in self.answerView.subviews) {
[answerBtn removeFromSuperview];
}
CGFloat answerBtnW = 40;
CGFloat answerBtnH = answerBtnW;
CGFloat answerBtnY = 0;
CGFloat spaceXmargin = 10;
NSInteger answerLength = quesitonModel.answer.length;
CGFloat leftMargin = (self.view.frame.size.width - answerLength *answerBtnW - (answerLength - 1)*spaceXmargin)*0.5;
for (int i = 0; i <answerLength ; i++) {
// 3.1创建按钮
UIButton *answerBtn = [[UIButton alloc]init];
CGFloat answerBtnX = leftMargin + (answerBtnW + spaceXmargin)*i;
// 3.2设置frame
answerBtn.frame = CGRectMake(answerBtnX, answerBtnY, answerBtnW, answerBtnH);
// 3.3添加
[self.answerView addSubview:answerBtn];
// 3.4设置背景图片
[answerBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
[answerBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
// 3.5修改文字的颜色
[answerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 3.6答案按钮添加点击事件
[answerBtn addTarget: self action:@selector(answerBtn:) forControlEvents:UIControlEventTouchUpInside];
}
// 4.添加带选项按钮
// 4.0 移除上一次的待选项按钮
for (UIButton *optionBtn in self.optionView.subviews) {
[optionBtn removeFromSuperview];
}
NSInteger optionLength = quesitonModel.options.count;
CGFloat optionW = 40;
CGFloat optionH = optionW;
CGFloat spaceOptionX = 10;
CGFloat topMargin = 30;
CGFloat spaceOptionY =15;
int totalColumn = 7;
CGFloat optionXmargin = (self.view.frame.size.width - totalColumn * optionW - (totalColumn - 1)* spaceOptionX)*0.5;
for (int i = 0; i < optionLength; i++) {
// 4.1创建待选项按钮
UIButton *optionBtn = [[UIButton alloc]init];
int col = i % totalColumn;
int row = i / totalColumn;
CGFloat optionX = optionXmargin + (optionW + spaceOptionX)*col;
CGFloat optionY = topMargin + (optionH + spaceOptionY)*row;
// 4.2设置frame
optionBtn.frame = CGRectMake(optionX, optionY, optionW, optionH);
// 4.3 添加
[self.optionView addSubview:optionBtn];
// 4.4 设置背景图片
[optionBtn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
[optionBtn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
// 4.5设置文字
[optionBtn setTitle:quesitonModel.options[i] forState:UIControlStateNormal];
// 4.6修改文字颜色
[optionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 4.7待选项按钮添加点击事件
[optionBtn addTarget:self action:@selector(optionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
/**
* 答案按钮点击
*/
- (void)answerBtn:(UIButton *)answerBtn{
// 1.让对应的待选项按钮显示
for (UIButton *optionBtn in self.optionView.subviews) {
// 1.1取得每一个按钮的文字
NSString *optionTitle = [optionBtn titleForState:UIControlStateNormal];
if ([optionTitle isEqual:[answerBtn titleForState:UIControlStateNormal]]&& optionBtn.hidden == YES) {//找到对应的待选项按钮
optionBtn.hidden = NO;
break;
}
}
// 2.让点击的答案按钮文字消失
[answerBtn setTitle:nil forState:UIControlStateNormal];
// 3.让按钮文字显示黑色
for (UIButton *answerBtn in self.answerView.subviews) {
[answerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
}
/**
* 待选项按钮点击
*/
- (void)optionBtnClick:(UIButton *)optionBtn{
// 1.让点击的按钮隐藏
optionBtn.hidden = YES;
// 2.让答案按钮中第一个没有文字的按钮显示对应文字
NSString *optionTitle = [optionBtn titleForState:UIControlStateNormal];
for (UIButton *answerBtn in self.answerView.subviews) {
NSString *answerTitle = [answerBtn titleForState:UIControlStateNormal];
if (answerTitle.length == 0) {//表示找到答案按钮中第一个没有文字的按钮
[answerBtn setTitle:optionTitle forState:UIControlStateNormal];
break;
}
}
// 3.确定答案是否满了
BOOL full = YES;
NSMutableString *tempString = [NSMutableString string];
for (UIButton *answerBtn in self.answerView.subviews) {
NSString *answerTitle = [answerBtn titleForState:UIControlStateNormal];
if (answerTitle.length == 0) {
full = NO;
}
if (answerTitle) {
[tempString appendString:answerTitle];
}
}
// 4.判断答案是否正确
QuestionModel *questionModel = self.questions[self.index];
NSString *answer = questionModel.answer;
if (full) {
if ([tempString isEqualToString:answer]) {//答对了
// NSLog(@"答对了");
// 4.1 文字颜色变为蓝色
for (UIButton *answerBtn in self.answerView.subviews) {
[answerBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}
// 4.2加分
[self changeScore:1000];
// 4.3自动跳到下一题
[self performSelector:@selector(next) withObject:nil afterDelay:0.5];
}else{
// NSLog(@"错了");
for (UIButton *answerBtn in self.answerView.subviews) {
[answerBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
}
}
/**
* 大图功能
*/
- (IBAction)big {
// 1.添加阴影
// 1.1创建阴影
UIButton *cover = [[UIButton alloc]initWithFrame:self.view.bounds];
// 1.2添加
[self.view addSubview:cover];
// 1.3设置颜色
cover.backgroundColor = [UIColor blackColor];
// 1.4设置透明度
cover.alpha = 0.0;
self.cover = cover;
// 1.5阴影添加点击事件
[cover addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];
// 2.调整阴影和头像的位置
[self.view bringSubviewToFront:self.head];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
cover.alpha = 0.6;
// 3.修改头像的frame
CGFloat headX = 0;
CGFloat headW = self.view.frame.size.width;
CGFloat headH = headW;
CGFloat headY = (self.view.frame.size.height - headH)*0.5;
self.head.frame = CGRectMake(headX, headY, headW, headH);
[UIView commitAnimations];
}
/**
* 小图
*/
- (void)smallImage{
1.图片缩小
// self.head.frame = CGRectMake(123, 177, 120, 120);
2.阴影消失
// [self.cover removeFromSuperview];
//
[UIView animateWithDuration:1.0 animations:^{
// 1.图片缩小
self.head.frame = CGRectMake(123, 177, 120, 120);
self.cover.alpha = 0.0;
} completion:^(BOOL finished) {//动画完成后让阴影消失
// 2.阴影消失
[self.cover removeFromSuperview];
}];
}
/**
* 头像点击
*/
- (IBAction)headClick {
if (self.cover == nil) {// 表示没有阴影,变大
[self big];
}else{//有阴影,变小
[self smallImage];
}
}
/**
* 修改分数
*/
- (void)changeScore:(int)delta{
int score = [[self.scoreBtn titleForState:UIControlStateNormal]intValue ];
[self.scoreBtn setTitle:[NSString stringWithFormat:@"%d",score + delta] forState:UIControlStateNormal];
}