标签:
#import "ViewController.h"
#import "WDJQuestions.h"
@interface ViewController ()
/**
* 显示金币
*/
@property (weak, nonatomic) IBOutlet UIButton *moneyBtn;
/**
* 索引
*/
@property (weak, nonatomic) IBOutlet UILabel *lblIndex;
/**
* 图片名称
*/
@property (weak, nonatomic) IBOutlet UILabel *lblDec;
/**
* 图片展示按钮
*/
@property (weak, nonatomic) IBOutlet UIButton *imageBtn;
/**
* 答案展示区
*/
@property (weak, nonatomic) IBOutlet UIView *answerView;
/**
* 选项展示区
*/
@property (weak, nonatomic) IBOutlet UIView *optionView;
/**
* 下一题按钮
*/
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
/**
* 模型数据
*/
@property (nonatomic, strong) NSArray *questionsData;
/**
* 索引值
*/
@property (nonatomic, assign) NSInteger index;
/**
* 原图片按钮frame
*/
@property (nonatomic, assign) CGRect oldImageBtnFrame;
/**
* 阴影按钮
*/
@property (nonatomic, weak) UIButton *backgroundBtn;
/**
* 提示标示
*/
@property (nonatomic, assign) BOOL prompt;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_index = 0;
[self showUIView];
}
#pragma mark -
#pragma mark - 下一题按钮
/**
* 下一题按钮点击
*/
- (IBAction)nextBtnClick {
// 下一题的时候,要打开用户交互
_optionView.userInteractionEnabled = YES;
_index++;
// 提示变为no
_prompt = NO;
// 添加下一题按钮点击事件
[self showUIView];
}
#pragma mark -
#pragma mark - UI显示界面
- (void)showUIView {
// 判断下标是否越界
if (_index >= self.questionsData.count) {
return;
}
// 实例化模型
WDJQuestions *quesData = self.questionsData[_index];
// 索引显示
_lblIndex.text = [NSString stringWithFormat:@"%zd / %zd",_index+1,self.questionsData.count];
// 标题显示
_lblDec.text = quesData.title;
// 图片显示
[_imageBtn setImage:[UIImage imageNamed:quesData.icon] forState:UIControlStateNormal];
// 答案按钮展示
[self answerView:quesData];
// 选项区按钮展示
[self optionsView:quesData];
// 如果是最后一题,就禁用
self.nextBtn.enabled = ((_index+1) != self.questionsData.count);
}
#pragma mark -
#pragma mark - 答案展示区
- (void)answerView:(WDJQuestions *)quesData {
// 调用之前,把上一次的删掉
[self.answerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// 获取答案按钮个数
NSInteger ansCount = quesData.answer.length;
// 按钮大小
NSInteger ansBtnW = 40;
NSInteger ansBtnH = ansBtnW;
// 按钮间距
NSInteger margin = 10;
// 左间距
CGFloat leftMargin = (self.answerView.bounds.size.width - ansCount * ansBtnW - (ansCount - 1) * margin) * 0.5;
// 循环创建按钮
for (NSInteger i = 0; i < ansCount; i++) {
// 按钮位置
// ansBtnX
CGFloat ansBtnX = leftMargin + (ansBtnW + margin) * i;
// ansBtnY
CGFloat ansBtnY = 0;
// 实例化按钮
UIButton *ansBtn = [[UIButton alloc] initWithFrame:CGRectMake(ansBtnX, ansBtnY, ansBtnW, ansBtnH)];
// 按钮背景t图
[ansBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
[ansBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
// 按钮文字颜色
[ansBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 添加答题按钮点击方法
[ansBtn addTarget:self action:@selector(answerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
// 添加到ansView中
[self.answerView addSubview:ansBtn];
}
}
#pragma mark -
#pragma mark - 答题区点击按钮方法
- (void)answerBtnClick:(UIButton *)answerBtn {
// 点击之后,打开选项区用户交互
_optionView.userInteractionEnabled = YES;
// 按钮文字删除
[answerBtn setTitle:nil forState:UIControlStateNormal];
// 点击之后答案按钮颜色变为黑色
[_answerView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 类型转换
UIButton *ansBtn = obj;
// 变色
[ansBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}];
// 把对应的选项区的按钮显示
[_optionView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 类型转换
UIButton *optBtn = obj;
if (optBtn.tag == answerBtn.tag) {
optBtn.hidden = NO;
*stop = YES;
}
}];
answerBtn.tag = 0;
}
#pragma mark -
#pragma mark - 选项展示区
- (void)optionsView:(WDJQuestions *)quesData {
// 调用之前删除之前的空间
[self.optionView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// 列数
NSInteger cols = 7;
// 选项个数
NSInteger quesDataCount = quesData.options.count;
// 按钮大小
NSInteger optionBtnW = 40;
NSInteger optionBtnH = optionBtnW;
// 按钮间距
NSInteger margin = 10;
// 左间距
CGFloat leftMargin = (self.optionView.bounds.size.width - optionBtnW * cols - (cols - 1) * margin) * 0.5;
CGFloat topMargin = 0;
// 循环创建按钮
for (NSInteger i = 0; i < quesDataCount; i++) {
// 行列索引
NSInteger colsNum = i % cols;
NSInteger rowsNum = i / cols;
// x坐标
CGFloat optionBtnX = leftMargin + (optionBtnW + margin) * colsNum;
// y坐标
CGFloat optionBtnY = topMargin + (optionBtnH + margin) * rowsNum;
// 设置frame
UIButton *optionBtn = [[UIButton alloc] initWithFrame:CGRectMake(optionBtnX, optionBtnY, optionBtnW, optionBtnH)];
// 背景图
[optionBtn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
[optionBtn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
// 按钮tag值
optionBtn.tag = i+1;
// 按钮文字颜色
[optionBtn setTitle:quesData.options[i] forState:UIControlStateNormal];
[optionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// 添加选项区事件监听
[optionBtn addTarget:self action:@selector(optionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
// 添加到view中
[self.optionView addSubview:optionBtn];
}
}
#pragma mark -
#pragma mark - 选项区点击按钮方法
- (void)optionBtnClick:(UIButton *)optionBtn {
// 按钮隐藏
optionBtn.hidden = YES;
// 取出按钮文字
NSString *title = optionBtn.currentTitle;
// 把按钮文字放到答案区
[_answerView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 类型转换
UIButton *ansBtn = obj;
if (ansBtn.currentTitle == nil) {
[ansBtn setTitle:title forState:UIControlStateNormal];
ansBtn.tag = optionBtn.tag;
*stop = YES;
}
}];
// 定义变量标示,判断是否答题完成
__block BOOL finshAns = YES;
// 定义可变字符串,存储答题区字符串
NSMutableString *Mstr = [NSMutableString string];
// 遍历答题区
[_answerView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIButton *ansBtn = obj;
if (ansBtn.currentTitle == nil) {
finshAns = NO;
*stop = YES;
} else {
NSString *str = ansBtn.currentTitle;
[Mstr appendString:str];
}
}];
// 判断
if (finshAns == YES) {
// 关闭用户交互
_optionView.userInteractionEnabled = NO;
// 获取正确答案
WDJQuestions *quesData = self.questionsData[_index];
// 比较
if ([quesData.answer isEqualToString:Mstr]) {
if (_index == 9) {
// 最后一题的时候跳转第一题
_index = -1;
}
// 跳转下一题
[self performSelector:@selector(nextBtnClick) withObject:nil afterDelay:2.0];
// 文字设置为蓝色
[_answerView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIButton *ansBtn = obj;
[ansBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}];
// 金币+100
[self newMoney:100];
if (!self.nextBtn.enabled) {
[self helpBtnClick];
}
} else {
// 文字设置为红色
[_answerView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIButton *ansBtn = obj;
[ansBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}];
// 金币-1000
[self newMoney:-1000];
}
}
}
#pragma mark 计算金币
- (void)newMoney:(NSInteger)var {
// 获取当前金币
NSInteger currentMon = _moneyBtn.currentTitle.intValue;
// 操作金币
NSInteger nowMon = currentMon + var;
// 赋值给按钮
[_moneyBtn setTitle:[NSString stringWithFormat:@"%zd",nowMon] forState:UIControlStateNormal];
}
#pragma mark -
#pragma mark - 点击图片变大
- (IBAction)bigBtnClick {
// 原尺寸获取
_oldImageBtnFrame = self.imageBtn.frame;
// 设置新的尺寸
NSInteger imageBtnW = self.view.bounds.size.width;
NSInteger imageBtnH = imageBtnW;
// 设置坐标
NSInteger imageBtnX = 0;
CGFloat imageBtnY = (self.view.bounds.size.height - imageBtnH) * 0.5;
// 创建背景按钮图片
UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
// 背景图
[backBtn setBackgroundImage:[UIImage imageNamed:@"bj"] forState:UIControlStateNormal];
self.backgroundBtn = backBtn;
// 设置透明度
backBtn.alpha = 0;
// 添加到view上
[self.view addSubview:self.backgroundBtn];
// 把图片置顶
[self.view bringSubviewToFront:self.imageBtn];
// 设置动画
[UIView animateWithDuration:1.0 animations:^{
self.imageBtn.frame = CGRectMake(imageBtnX, imageBtnY, imageBtnW, imageBtnH);
self.backgroundBtn.alpha = 0.7;
} completion:^(BOOL finished) {
[self.backgroundBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
}];
}
#pragma mark 阴影点击方法
- (void)backBtnClick {
// 图片变小
[UIView animateWithDuration:1.0 animations:^{
self.imageBtn.frame = _oldImageBtnFrame;
self.backgroundBtn.alpha = 0;
} completion:^(BOOL finished) {
[self.backgroundBtn removeFromSuperview];
}];
}
#pragma mark -
#pragma mark - 点击图片变大
- (IBAction)imageBtnClick {
// 判断是否存在阴影
if (self.backgroundBtn) {
[self backBtnClick];
}else {
[self bigBtnClick];
}
}
#pragma mark -
#pragma mark - 点击提示按钮事件
- (IBAction)promptBtnClick {
// 判断是否提示过
if (_prompt) {
return;
}
_prompt = YES;
// 金币减少
[self newMoney:-1000];
// 模拟用户点击了答案区域的按钮
[_answerView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 类型转换
UIButton *ansBtn = obj;
// 模拟点击
[self answerBtnClick:ansBtn];
}];
// 获取答案
WDJQuestions *quesData = self.questionsData[_index];
// 截取正确答案的第一个字符
NSString *fistAns = [quesData.answer substringToIndex:1];
// 判断这个文字与选项区的答案
[_optionView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 类型转换
UIButton *optBtn = obj;
// 判断
if ([optBtn.currentTitle isEqualToString:fistAns]) {
// 模拟点击选项按钮
[self optionBtnClick:optBtn];
*stop = YES;
}
}];
}
#pragma mark -
#pragma mark - 帮助按钮点击事件
- (IBAction)helpBtnClick {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"恭喜你通关了" message:@"请拨打110领取奖金" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alertView show];
}
#pragma mark -
#pragma mark - 懒加载方法
- (NSArray *)questionsData {
if (_questionsData == nil) {
// 获取路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil];
// 定义数组存储数据
NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];
// 定义可变数组
NSMutableArray *Marr = [NSMutableArray arrayWithCapacity:tempArr.count];
// 字典转模型
for (NSDictionary *dict in tempArr) {
// 实例化模型
WDJQuestions *queData = [WDJQuestions questionWithDict:dict];
// 存入到可变数组中
[Marr addObject:queData];
}
// 赋值
_questionsData = Marr;
}
return _questionsData;
}
@end
标签:
原文地址:http://www.cnblogs.com/qhsblogs/p/5515877.html