----------------------------Model声明----------------------------
#import <Foundation/Foundation.h>
@interface CZQuestion : NSObject
//正确答案
@property (nonatomic,copy) NSString *answer;
//图片
@property (nonatomic,copy) NSString *icon;
//标题
@property (nonatomic,copy) NSString *title;
//答案选项数组
@property (nonatomic,strong) NSArray *options;
//提供类方法返回所有模型数据
+ (NSArray *) questions;
//类方法获取当前对象
+ (instancetype) questionWithDic:(NSDictionary *)dic;
//对象方法获取当前实例对象
- (instancetype) initWithDic:(NSDictionary *)dic;
@end
#import "CZQuestion.h"
@implementation CZQuestion
- (instancetype)initWithDic:(NSDictionary *)dic
{
if(self=[super init])
{
self.answer=dic[@"answer"];
self.icon=dic[@"icon"];
self.title=dic[@"title"];
self.options=dic[@"options"];
}
return self;
}
+ (instancetype)questionWithDic:(NSDictionary *)dic
{
return [[self alloc] initWithDic:dic];
}
+ (NSArray *)questions
{
//1.先获取字典数组
NSArray *sourceArr=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil]];
//创建可变的模型数组
NSMutableArray *desArr=[NSMutableArray array];
//2.循环遍历字典数组,字典转模型
for (NSDictionary *dic in sourceArr) {
//每一个字典对应着一个新的模型对象
CZQuestion *ques=[CZQuestion questionWithDic:dic];
[desArr addObject:ques];
}
//3.返回模型数组
return desArr;
}
@end
#import "ViewController.h"
#import "CZQuestion.h"
#define cols 7
@interface ViewController () <UIAlertViewDelegate>
//模型数组
@property (nonatomic,strong) NSArray *questions;
//题目序号
@property (weak, nonatomic) IBOutlet UILabel *pageView;
//题目描述
@property (weak, nonatomic) IBOutlet UILabel *titleView;
//图片
@property (weak, nonatomic) IBOutlet UIButton *iconView;
//下一题按钮
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
//分数
@property (weak, nonatomic) IBOutlet UIButton *scoreView;
//用户答案区域view
@property (weak, nonatomic) IBOutlet UIView *answerBtnView;
//答案选项区域view
@property (weak, nonatomic) IBOutlet UIView *answerOptionBtnView;
//题目索引
@property (nonatomic,assign) int index;
//下一题
- (IBAction)bigImage;
//点击图片:如果是大图就变小图,如果是小图就变大图
- (IBAction)iconVIewClick;
//下一题
- (IBAction)next;
//创建全局的阴影控件对象
@property (nonatomic,weak) UIButton *shade;
//存储iconView最原始的frame
@property (nonatomic,assign) CGRect iconTempFrame;
//提示
- (IBAction)tip;
@end
@implementation ViewController
//懒加载
- (NSArray *)questions
{
if (_questions==nil) {
_questions=[CZQuestion questions];
}
return _questions;
}
//打开界面,显示第一题
- (void)viewDidLoad {
[super viewDidLoad];
self.index=-1;
//一开始的时候将iconView的frame存储到全局的变量中,供以后使用。
self.iconTempFrame=self.iconView.frame;
[self next];
}
//大图
- (IBAction)bigImage {
//1.让图片变大
CGFloat iconViewW=self.view.frame.size.width;
CGFloat iconViewH=iconViewW;
CGFloat iconViewX=0;
CGFloat iconViewY=(self.view.frame.size.height-iconViewW)*0.5;
//2.添加阴影
UIButton *shade=[[UIButton alloc] init];
//将当前全局的shade对象指针局部的shade阴影对象
self.shade=shade;
//shade.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
shade.frame=self.view.bounds;
//设置阴影的颜色
shade.backgroundColor=[UIColor blackColor];
//设置透明度
shade.alpha=0;//目的就是为了生成动画效果
//为阴影 添加点击事件
[shade addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:shade];
//3.让图片调整位置到最前面
[self.view bringSubviewToFront:self.iconView];
//为阴影的出现 和图片的frame'的变化添加动画效果
[UIView animateWithDuration:0.5 animations:^{
//让图片变大就是修改图片的frame
self.iconView.frame=CGRectMake(iconViewX, iconViewY, iconViewW, iconViewH);
//修改阴影的透明度 0完全透明 1是完全不透明
shade.alpha=0.8;
}];
}
- (IBAction)iconVIewClick {
// if(self.iconView.frame.size.width==self.view.frame.size.width)
// {
// [self smallImage];
// }
// else
// {
// [self bigImage];
// }
if (self.shade) {
[self smallImage];
}
else
{
[self bigImage];
}
}
//大图变小图
- (void) smallImage
{
[UIView animateWithDuration:0.5 animations:^{
//1.让阴影变透明
self.shade.alpha=0;
//2.让图片恢复到原始的frame
self.iconView.frame=self.iconTempFrame;
} completion:^(BOOL finished) {
//3.移除阴影
[self.shade removeFromSuperview];
}];
}
//下一题
- (IBAction)next {
self.index++;
//加载 基本信息
[self loadBaseInfo];
//加载 用户答案区域按钮
[self loadAnswerBtn];
//加载 答案选项区域按钮
[self loadAnswerOptionBtn];
//判断『下一题』是否禁用
if (self.index==self.questions.count-1) {
self.nextBtn.enabled=NO;
}
}
//封装基本数据
- (void) loadBaseInfo
{
CZQuestion *question=self.questions[self.index];
self.pageView.text=[NSString stringWithFormat:@"%d/%ld",self.index+1,self.questions.count];
self.titleView.text=question.title;
[self.iconView setImage:[UIImage imageNamed:question.icon] forState:UIControlStateNormal];
}
//加载答案按钮
- (void) loadAnswerBtn
{
//清除上次添加的答案按钮,否则会进行叠加
for (UIButton *btn in self.answerBtnView.subviews) {
//将当前 遍历的按钮从父控件中移除
[btn removeFromSuperview];
}
//1获取当前模型数据
CZQuestion *question=self.questions[self.index];
//2.获取当前模型数据中的正确答案
NSString *answer=question.answer;
//3.获取正确答案的长度
NSInteger cnt=answer.length;
//按钮的宽高
CGFloat padding=10;
CGFloat btnW=44;
CGFloat btnH=btnW;
CGFloat btnX=0;
CGFloat paddingLeft=(self.answerBtnView.frame.size.width-cnt*btnW- (cnt-1)*padding)*0.5;
//4.根据答案的长度代码创建按钮
for (int i=0; i<cnt; i++) {
//4.1创建按钮对象
UIButton *btn=[[UIButton alloc] init];
//4.2设置按钮的Frame
btnX=paddingLeft+i*(btnW+padding);
btn.frame=CGRectMake(btnX, 0, btnW, btnH);
//为按钮添加背景图片
[btn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
//设置文本颜色
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//为用户答案按钮添加点击事件
[btn addTarget:self action:@selector(answerBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
//4.3将按钮添加到答案区域中
[self.answerBtnView addSubview:btn];
}
}
//加载答案选项区域按钮
- (void) loadAnswerOptionBtn
{
//清除上次添加的答案按钮,否则会进行叠加
for (UIButton *btn in self.answerOptionBtnView.subviews) {
//将当前 遍历的按钮从父控件中移除
[btn removeFromSuperview];
}
//1获取当前模型数据
CZQuestion *question=self.questions[self.index];
//2.获取当前模型数据中的正确答案
NSArray *options=question.options;
//3.获取正确答案的长度
NSInteger cnt=options.count;
//按钮的宽高
CGFloat btnW=44;
CGFloat btnH=btnW;
CGFloat padding=(self.answerOptionBtnView.frame.size.width-cols*btnW)/(cols+1);
CGFloat btnX=0;
CGFloat btnY=0;
//4.循环创建答案选项按钮
for (int i=0; i<cnt; i++) {
UIButton *btn=[[UIButton alloc] init];
btnX=padding+(btnW+padding)*(i%cols);
btnY=padding+(btnH+padding)*(i/cols);
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 setTitle:options[i] forState:UIControlStateNormal];
//设置文本颜色
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//为答案选项按钮添加点击事件.我们需要将当前 按钮做为参数传递
[btn addTarget:self action:@selector(optionBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
//添加按钮到答案选项区域
[self.answerOptionBtnView addSubview:btn];
}
}
//点击用户答案区域按钮
- (void) answerBtnDidClick:(UIButton *)btn
{
//1。获取文本值
NSString *text=btn.currentTitle;
//2.遍历选项区域按钮,将能够匹配文本值且是隐藏状态的按钮重新显示
for (UIButton *optBtn in self.answerOptionBtnView.subviews) {
if ([text isEqualToString:optBtn.currentTitle] && optBtn.hidden) {
optBtn.hidden=NO;
break;
}
}
//3.将当前按钮的文本清除
[btn setTitle:nil forState:UIControlStateNormal];
}
//点击答案选项按钮
- (void) optionBtnDidClick:(UIButton *)btn
{
//1.判断答案是否填充满
if ([self answerBtnIsFull]) {
return;
}
//2.获取当前点击的按钮的文本,填充到用户答案区域中第一个为文本nil的按钮上
//currentTitle可以获取当前按钮的文本
NSString *text=btn.currentTitle;
btn.hidden=YES;
//遍历用户答案区域按钮
for (UIButton *anBtn in self.answerBtnView.subviews) {
//NSLog(@"-%@-",anBtn.currentTitle);
if (anBtn.currentTitle==nil) {
[anBtn setTitle:text forState:UIControlStateNormal];
break;
}
}
if ([self answerBtnIsFull]) {//点击完这一次之后,有可能答案填充满,那么需要进行答案是否正确的判断
//3.再次判断答案是否正确,如果正确,则:
//取出正确答案
CZQuestion *question=self.questions[self.index];
NSString *answer=question.answer;
//拼接用户答案
NSMutableString *userAnswer=[NSMutableString string];
for (UIButton *btn in self.answerBtnView.subviews) {
[userAnswer appendString:btn.currentTitle];
}
//判断答案是否正确
if ([answer isEqualToString:userAnswer]) {
//3.1判断是否是最后一题,如果 是则给出提示
if (self.index==self.questions.count-1) {
//3.1.1创建提示框
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"恭喜通关" message:@"请选择你想进行的下一步操作" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"再来一次", nil];
//3.1.2.显示弹出消息框
[alert show];
}
//3.2如果 不是,则自动跳转到下一题
else
{
//延迟操作
[self performSelector:@selector(next) withObject:nil afterDelay:0.5];
}
}
}
}
/**
* 判断用户答案区域文本是否已经填充满
*/
- (BOOL) answerBtnIsFull
{
for (UIButton *anBtn in self.answerBtnView.subviews)
{
if(anBtn.currentTitle ==nil)
{
return NO;
}
}
return YES;
}
//提示
- (IBAction)tip {
//1.清除用户已经填充的答案,同时将答案文本对应的按钮在答案选项区域重新显示
for (UIButton *btn in self.answerBtnView.subviews) {
//清除现在按钮的文本,还原选项区域对应按钮的显示状态
[self answerBtnDidClick:btn];
}
//2.获取正确答案的第一个字符,查询到答案选项区域的某个按钮,模拟它个按钮的点击操作
//2.1先获取 到模型
CZQuestion *question=self.questions[self.index];
//2.2获取正确答案
NSString *answer=question.answer;
//2.3获取正确答案的第一个字符
NSString *first=[answer substringToIndex:1];
//2.4遍历答案选项区域按钮,查询出对应文本值的按钮,实现这个按钮的点击操作
for (UIButton *btn in self.answerOptionBtnView.subviews) {
if ([first isEqualToString:btn.currentTitle]) {
[self optionBtnDidClick:btn];
break;
}
}
}
#pragma mark UIAlertViewDelegate代理方法
//当你点击UIAlertView上的任何一个按钮的时候调用
//buttonIndex就是你所点击的当前按钮的索引
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"%ld",buttonIndex);
//按钮的索引由添加的顺序来决定的
if (buttonIndex==1) {
//再来一次
self.index=-1;
//重新启用下一题按钮
self.nextBtn.enabled=YES;
[self next];
}
else if(buttonIndex==2)
{
exit(0);//退出
}
}
@end