1.设计上半部分界面及懒加载数据并实现下一题按钮

1、创建数据模型类FRQuestion
FRQuestion.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface FRQuestion : NSObject
@property(nonatomic,copy)NSString* answer;
@property(nonatomic,copy)NSString* icon;
@property(nonatomic,copy)NSString* title;
@property(nonatomic,copy)NSArray* options;

-(instancetype)initWithDict:(NSDictionary*) dict;
+(instancetype)questionWithDict:(NSDictionary*) dict;

@end

NS_ASSUME_NONNULL_END

FRQuestion.m

#import "FRQuestion.h"

@implementation FRQuestion
-(instancetype)initWithDict:(NSDictionary*) dict{
  if (self=[super init]) {
    self.answer=dict[@"answer"];
    self.title=dict[@"title"];
    self.icon=dict[@"icon"];
    self.options=dict[@"options"];
  }
  return self;
}
+(instancetype)questionWithDict:(NSDictionary*) dict{
  return [[self alloc]initWithDict:dict];
}


@end

2、对应的plist文件
在这里插入图片描述
3、修改控制器类ViewController
ViewController.m

#import "ViewController.h"
#import "FRQuestion.h"
@interface ViewController ()
//所有问题数据都保存在这个数组中
@property(nonatomic,copy)NSArray* questions;//将来用来存放模型的一个数组
//控制题目索引
@property(nonatomic,assign)int index;
@property (weak, nonatomic) IBOutlet UILabel *lblIndex;
@property (weak, nonatomic) IBOutlet UIButton *btnScore;
@property (weak, nonatomic) IBOutlet UILabel *lbltitle;
@property (weak, nonatomic) IBOutlet UIButton *btnIcon;
@property (weak, nonatomic) IBOutlet UIButton *btnNext;
- (IBAction)btnNextClick;




@end

@implementation ViewController
//重写方法,设置状态栏为高亮色
-(UIStatusBarStyle)preferredStatusBarStyle{
  return UIStatusBarStyleLightContent;
}
//懒加载数据,重写属性NSArray* questions的get方法
-(NSArray*)questions{
  if (_questions==nil) {
    //加载数据
    NSString* path=[[NSBundle mainBundle]pathForResource:@"questions.plist" ofType:nil];
    NSArray* arraydict=[NSArray arrayWithContentsOfFile:path];
    NSMutableArray* arrayModelM=[NSMutableArray array];
    for (NSDictionary* dict in arraydict) {
      FRQuestion* model=[FRQuestion questionWithDict:dict];
      [arrayModelM addObject:model];
    }
    _questions=arrayModelM;
  }
  return _questions;
}
//隐藏状态栏
-(BOOL)prefersStatusBarHidden{
  return NO;//不隐藏
}


- (void)viewDidLoad {
  [super viewDidLoad];
  //默认初始化显示第一题
  self.index=-1;
  //调用下一题方法
  [self nextQuestion];
}

//点击访问下一题
- (IBAction)btnNextClick {
  //调用下一题方法
  [self nextQuestion];
}
//显示下一题方法
-(void)nextQuestion{
  //1.让索引++
  self.index++;
  NSLog(@"%d",self.index);
  //2.获取索引当前模型数据
  FRQuestion* model=self.questions[self.index];
  //3.把模型数据设置到界面对应的控件上
  self.lblIndex.text=[NSString stringWithFormat:@"%d/%ld",(self.index+1),self.questions.count];
  self.lbltitle.text=model.title;
  [self.btnIcon setImage:[UIImage imageNamed:model.icon] forState:UIControlStateNormal];
  //4.到达最后一题以后,禁用“下一题”按钮
  if (self.index==self.questions.count-1) {
    _btnNext.enabled=NO;
  }
  
}
@end

在这里插入图片描述

2.实现大图切换和大小图变化,背景变化
#import "ViewController.h"
#import "FRQuestion.h"
@interface ViewController ()
//所有问题数据都保存在这个数组中
@property(nonatomic,copy)NSArray* questions;//将来用来存放模型的一个数组
//控制题目索引
@property(nonatomic,assign)int index;
@property (weak, nonatomic) IBOutlet UILabel *lblIndex;
@property (weak, nonatomic) IBOutlet UIButton *btnScore;
@property (weak, nonatomic) IBOutlet UILabel *lbltitle;
@property (weak, nonatomic) IBOutlet UIButton *btnIcon;
@property (weak, nonatomic) IBOutlet UIButton *btnNext;

@property (nonatomic,assign) CGRect iconFrame;//结构体属性,头像按钮的原始大小
@property (weak, nonatomic)  UIButton *cover;//用来引用大图中的阴影

//下一题按钮单击事件
- (IBAction)btnNextClick;
//大图按钮单击事件
- (IBAction)bigImage:(id)sender;
//头像按钮单击事件
- (IBAction)btnIconClick:(id)sender;




@end

@implementation ViewController
//重写方法,设置状态栏为高亮色
-(UIStatusBarStyle)preferredStatusBarStyle{
  return UIStatusBarStyleLightContent;
}
//懒加载数据,重写属性NSArray* questions的get方法
-(NSArray*)questions{
  if (_questions==nil) {
    //加载数据
    NSString* path=[[NSBundle mainBundle]pathForResource:@"questions.plist" ofType:nil];
    NSArray* arraydict=[NSArray arrayWithContentsOfFile:path];
    NSMutableArray* arrayModelM=[NSMutableArray array];
    for (NSDictionary* dict in arraydict) {
      FRQuestion* model=[FRQuestion questionWithDict:dict];
      [arrayModelM addObject:model];
    }
    _questions=arrayModelM;
  }
  return _questions;
}
//隐藏状态栏
-(BOOL)prefersStatusBarHidden{
  return NO;//不隐藏
}


- (void)viewDidLoad {
  [super viewDidLoad];
  //默认初始化显示第一题
  self.index=-1;
  //调用下一题方法
  [self nextQuestion];
}


//点击大图按钮响应的方法
- (IBAction)btnIconClick:(id)sender {
  if (self.cover==nil) {
    [self bigImage:nil];
  }else{
    [self smallImage];
  }
}

- (IBAction)bigImage:(id)sender {
  //0.先记录下头像按钮的原始大小
  self.iconFrame=self.btnIcon.frame;
  
  //1、创建大小与self.view一样的按钮,把这个按钮作为一个阴影
  UIButton* btnCover=[[UIButton alloc]init];
  //设置按钮大小
  btnCover.frame=self.view.bounds;
  btnCover.backgroundColor=[UIColor blackColor];
  btnCover.alpha=0.0;
  //把按钮加到selfview中
  [self.view addSubview:btnCover];
  
  //给阴影注册一个单击事件
  [btnCover addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];
  
  //2、把图片设置到阴影上方
  [self.view bringSubviewToFront:self.btnIcon];//把控件带到view的最上方
  //3、通过动画的方式吧图片变大
  CGFloat icoW=self.view.frame.size.width;
  CGFloat icoH=icoW;
  CGFloat icoX=0;
  CGFloat icoY=(self.view.frame.size.height-icoH)*0.5;
  //设置图片新的frame
  
  [UIView animateWithDuration:1 animations:^{
    btnCover.alpha=0.6;
    
    self.btnIcon.frame=CGRectMake(icoX, icoY, icoW, icoH);
    
  }];
  
  //通过self.cover来应用btncover
  self.cover=btnCover;
  
}

//阴影单击事件方法
-(void)smallImage{
  [UIView animateWithDuration:1 animations:^{
    
  }];
  [UIView animateWithDuration:1 animations:^{
    //1、设置头像尺寸还原,
    self.btnIcon.frame=self.iconFrame;
    
    //2.让阴影按钮的透明度变成0
    self.cover.alpha=0.0;
  } completion:^(BOOL finished) {
    //3、移除阴影按钮
    if (finished) {
      //移除阴影按钮
      [self.cover removeFromSuperview];
      //当图像变成小图的时候,再把self.cover设置成nil
      self.cover=nil;
    }

  }];
  

}
//点击访问下一题
- (IBAction)btnNextClick {
  //调用下一题方法
  [self nextQuestion];
}
//显示下一题方法
-(void)nextQuestion{
  //1.让索引++
  self.index++;
  NSLog(@"%d",self.index);
  //2.获取索引当前模型数据
  FRQuestion* model=self.questions[self.index];
  //3.把模型数据设置到界面对应的控件上
  self.lblIndex.text=[NSString stringWithFormat:@"%d/%ld",(self.index+1),self.questions.count];
  self.lbltitle.text=model.title;
  [self.btnIcon setImage:[UIImage imageNamed:model.icon] forState:UIControlStateNormal];
  //4.到达最后一题以后,禁用“下一题”按钮
  if (self.index==self.questions.count-1) {
    _btnNext.enabled=NO;
  }
  
}
@end

在这里插入图片描述

3.添加答案按钮、判断答案是否正确
#import "ViewController.h"
#import "FRQuestion.h"
@interface ViewController ()
//所有问题数据都保存在这个数组中
@property(nonatomic,copy)NSArray* questions;//将来用来存放模型的一个数组
//控制题目索引
@property(nonatomic,assign)int index;
@property (weak, nonatomic) IBOutlet UILabel *lblIndex;
@property (weak, nonatomic) IBOutlet UIButton *btnScore;
@property (weak, nonatomic) IBOutlet UILabel *lbltitle;
@property (weak, nonatomic) IBOutlet UIButton *btnIcon;
@property (weak, nonatomic) IBOutlet UIButton *btnNext;
@property (weak, nonatomic) IBOutlet UIView *answersView;
@property (weak, nonatomic) IBOutlet UIView *optionsView;





@property (nonatomic,assign) CGRect iconFrame;//结构体属性,头像按钮的原始大小
@property (weak, nonatomic)  UIButton *cover;//用来引用大图中的阴影

//下一题按钮单击事件
- (IBAction)btnNextClick;
//大图按钮单击事件
- (IBAction)bigImage:(id)sender;
//头像按钮单击事件
- (IBAction)btnIconClick:(id)sender;




@end

@implementation ViewController
//重写方法,设置状态栏为高亮色
-(UIStatusBarStyle)preferredStatusBarStyle{
  return UIStatusBarStyleLightContent;
}
//懒加载数据,重写属性NSArray* questions的get方法
-(NSArray*)questions{
  if (_questions==nil) {
    //加载数据
    NSString* path=[[NSBundle mainBundle]pathForResource:@"questions.plist" ofType:nil];
    NSArray* arraydict=[NSArray arrayWithContentsOfFile:path];
    NSMutableArray* arrayModelM=[NSMutableArray array];
    for (NSDictionary* dict in arraydict) {
      FRQuestion* model=[FRQuestion questionWithDict:dict];
      [arrayModelM addObject:model];
    }
    _questions=arrayModelM;
  }
  return _questions;
}
//隐藏状态栏
-(BOOL)prefersStatusBarHidden{
  return NO;//不隐藏
}


- (void)viewDidLoad {
  [super viewDidLoad];
  //默认初始化显示第一题
  self.index=-1;
  //调用下一题方法
  [self nextQuestion];
}


//点击大图按钮响应的方法
- (IBAction)btnIconClick:(id)sender {
  if (self.cover==nil) {
    [self bigImage:nil];
  }else{
    [self smallImage];
  }
}


//实现大图
- (IBAction)bigImage:(id)sender {
  //0.先记录下头像按钮的原始大小
  self.iconFrame=self.btnIcon.frame;
  
  //1、创建大小与self.view一样的按钮,把这个按钮作为一个阴影
  UIButton* btnCover=[[UIButton alloc]init];
  //设置按钮大小
  btnCover.frame=self.view.bounds;
  btnCover.backgroundColor=[UIColor blackColor];
  btnCover.alpha=0.0;
  //把按钮加到selfview中
  [self.view addSubview:btnCover];
  
  //给阴影注册一个单击事件
  [btnCover addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];
  
  //2、把图片设置到阴影上方
  [self.view bringSubviewToFront:self.btnIcon];//把控件带到view的最上方
  //3、通过动画的方式吧图片变大
  CGFloat icoW=self.view.frame.size.width;
  CGFloat icoH=icoW;
  CGFloat icoX=0;
  CGFloat icoY=(self.view.frame.size.height-icoH)*0.5;
  //设置图片新的frame
  
  [UIView animateWithDuration:1 animations:^{
    btnCover.alpha=0.6;
    
    self.btnIcon.frame=CGRectMake(icoX, icoY, icoW, icoH);
    
  }];
  
  //通过self.cover来应用btncover
  self.cover=btnCover;
  
}

//阴影单击事件方法
-(void)smallImage{
  
  [UIView animateWithDuration:1 animations:^{
    //1、设置头像尺寸还原,
    self.btnIcon.frame=self.iconFrame;
    
    //2.让阴影按钮的透明度变成0
    self.cover.alpha=0.0;
  } completion:^(BOOL finished) {
    //3、移除阴影按钮
    if (finished) {
      //移除阴影按钮
      [self.cover removeFromSuperview];
      //当图像变成小图的时候,再把self.cover设置成nil
      self.cover=nil;
    }

  }];
  

}

//点击访问下一题
- (IBAction)btnNextClick {
  //调用下一题方法
  [self nextQuestion];
}

//显示下一题方法
-(void)nextQuestion{
  //1.让索引++
  self.index++;
  
  //判断当前索引是否越界,如果越界,则提示用户
  if (self.index==self.questions.count) {
    //NSLog(@"答题完毕");
    
    return;
  }
  NSLog(@"%d",self.index);
  //2.获取索引当前模型数据
  FRQuestion*  model=self.questions[self.index];
  //根据模型设置数据
  [self settingData:model];
  
  //4.到达最后一题以后,禁用“下一题”按钮
  if (self.index==self.questions.count-1) {
    _btnNext.enabled=NO;
  }
  //动态创建答案按钮
  [self makeAnswerButtons:model];
  
  //动态生成待选按钮
  [self makeOptionsButton:model];
}


//把模型数据设置到界面上
-(void)settingData:(FRQuestion*) model{
  
  //3.把模型数据设置到界面对应的控件上
  self.lblIndex.text=[NSString stringWithFormat:@"%d/%ld",(self.index+1),self.questions.count];
  self.lbltitle.text=model.title;
  [self.btnIcon setImage:[UIImage imageNamed:model.icon] forState:UIControlStateNormal];
}

//动态创建答案按钮
-(void)makeAnswerButtons:(FRQuestion*)model{
  //清除所有的答案按钮
  /*
  while (self.answersView.subviews.firstObject) {
    [self.answersView.subviews.firstObject removeFromSuperview];
  }*/
  //让subview的每一个对象,分别调用一次removeFromSuperview方法,内部执行了循环,无需我们自己来循环
  [self.answersView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  
  
  //获取当前答案的文字
  NSUInteger len=model.answer.length;
  
  CGFloat margin=10;
  CGFloat btnAnswerW=40;
  CGFloat btnAnswerH=40;
  CGFloat btnAnswerY=0;
  CGFloat marginleft=(self.answersView.frame.size.width-len*btnAnswerW-margin*(len-1))/2;

  //循环创建答案按钮,有几个子就创建几个按钮
  for (int i=0; i<len; i++) {
    //创建一个按钮
    UIButton* btnAnswer=[[UIButton alloc]init];
    [btnAnswer setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //创建frame等
    [btnAnswer setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
    [btnAnswer setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateHighlighted];
    
    CGFloat btnAnswerX=marginleft+i*(btnAnswerW+margin);
    btnAnswer.frame=CGRectMake(btnAnswerX, btnAnswerY, btnAnswerW, btnAnswerH);

    //按钮加到answerView中去
    [self.answersView addSubview:btnAnswer];
    
    //为答案按钮创建单击是见
    [btnAnswer addTarget:self action:@selector(btnAnswerClick:) forControlEvents:UIControlEventTouchUpInside];
  }
  
}

//答案按钮点击事件,参数sender表示当前点击的对象
-(void)btnAnswerClick:(UIButton*)sender{
  //将待选按钮所在的view的交互启用
  self.optionsView.userInteractionEnabled=YES;
  
  //设置每个答案按钮文字为黑色
  [self setAnswerButtonColor:[UIColor blackColor] ];
  
  //找到待选按钮对应的位置,重新设置文字
  for (UIButton* optBtn in self.optionsView.subviews) {
    //判断比较待选按钮文字是否与当前被点击的答案按钮文字一致
    /*
    if ([sender.currentTitle isEqualToString:optBtn.currentTitle]) {
      optBtn.hidden=NO;
      break;
    }
    */
    if(sender.tag==optBtn.tag){
      optBtn.hidden=NO;
      break;
    }
    
  }
  
  //取消按钮文字,设置title为nil
  [sender setTitle:nil forState:UIControlStateNormal];
}
//动态生成待选按钮
-(void)makeOptionsButton:(FRQuestion*)model{
  //设置optionsView可以与用户交互
  [self.optionsView setUserInteractionEnabled:YES];
  //清除所有待选按钮view
  [self.optionsView.subviews
   makeObjectsPerformSelector:@selector(removeFromSuperview)];
  //获取待选按钮的文字的数组
  NSArray* words=model.options;
  
  //指定每个待选按钮的大小
  CGFloat margin=10;
  CGFloat optionW=40;
  CGFloat optionH=optionW;
  
  //指定列数
  int column=7;
  //计算出每行第一个按钮离左边的距离
  CGFloat marginLeft=(self.optionsView.frame.size.width-column*optionW-(column-1)*margin)/2;
  
  //根据待选文字循环创建待选按钮
  for (int i=0; i<words.count; i++) {
    //创建一个按钮并给每个按钮一个唯一的tag值
    UIButton* btnWord=[[UIButton alloc]init];
    btnWord.tag=i;
    //设置按钮背景、文字、frame
    [btnWord setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
    [btnWord setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];

    [btnWord setTitle:words[i] forState:UIControlStateNormal];
    [btnWord setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //计算当前按钮的列的索引和行的索引
    int colIndex=i%column;
    int rowIndex=i/column;
    
    CGFloat optionX=marginLeft+colIndex*(optionW+margin);
    CGFloat optionY=0+rowIndex*(optionH+margin);
    btnWord.frame=CGRectMake(optionX, optionY, optionW, optionH);
    //添加按钮到views上
    [self.optionsView addSubview:btnWord];
    
    //为待选按钮注册单击事件
    [btnWord addTarget:self action:@selector(optionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  }
  
}

//为待选的点击事件
-(void)optionButtonClick:(UIButton*)sender{
  //隐藏当前被点击按钮
  sender.hidden=YES;
  //把当前被点击的按钮的文字显示到第一个为空的“答案按钮上”
  //NSString* text=[sender titleForState:UIControlStateNormal];
  NSString* text=sender.currentTitle;//获取按钮当前状态下的文字
  
  //遍历答案按钮视图上的按钮
  for (UIButton* btnanswer in self.answersView.subviews) {
    if (btnanswer.currentTitle==nil) {
      //给当前点击的按钮的tag值也设置给答案按钮
      btnanswer.tag=sender.tag;
      //把当前点击的待选按钮的文字设置给对应的答案按钮
      [btnanswer setTitle:text forState:UIControlStateNormal];
      break;
    }
  }
  
  //判断答案按钮是否已经填满
  BOOL isFull=YES;
  //声明一个用来保存用户输入答案的字符串
  NSMutableString* userInput=[NSMutableString string];
  for (UIButton* btnanswer in self.answersView.subviews) {
    if (btnanswer.currentTitle==nil) {
      isFull=NO;
      break;
    }else{
      //拿到用户输入的字符串
      [userInput appendString:btnanswer.currentTitle];
    }
  }
  
  if (isFull) {
    //待选按钮的大view禁止交互
    self.optionsView.userInteractionEnabled=NO;
    //如果答案按钮被填满了,那么久判断用户点击输入的答案是否与标准答案一致,如果一致,则设置答案按钮的文字颜色为蓝色并在0.5秒跳转到下一题
    //获取当前题目的正确答案
    FRQuestion* model= self.questions[self.index];
    if ([model.answer isEqualToString:userInput]) {
      //设置所有的答案按钮的文字颜色为蓝色
      [self setAnswerButtonColor:[UIColor blueColor]];
      //延迟0.5秒后,跳转到下一题
      [self performSelector:@selector(nextQuestion) withObject:nil afterDelay:0.5];
    }else{
      //如果答案不一致(答案错误),则设置答案按钮文字为红色
      [self setAnswerButtonColor:[UIColor redColor]];
    }
  }
  
  
}
//设置答案按钮文字颜色
-(void)setAnswerButtonColor:(UIColor*) color{
  //遍历每个按钮并设置按钮标题文字
  for (UIButton* btnAnswer in self.answersView.subviews) {
    [btnAnswer setTitleColor:color forState:UIControlStateNormal];
  }
}

@end

在这里插入图片描述

4.实现加分、提示、设置图标和显示启动图片
#import "ViewController.h"
#import "FRQuestion.h"
@interface ViewController ()
//所有问题数据都保存在这个数组中
@property(nonatomic,copy)NSArray* questions;//将来用来存放模型的一个数组
//控制题目索引
@property(nonatomic,assign)int index;
@property (weak, nonatomic) IBOutlet UILabel *lblIndex;
@property (weak, nonatomic) IBOutlet UIButton *btnScore;
@property (weak, nonatomic) IBOutlet UILabel *lbltitle;
@property (weak, nonatomic) IBOutlet UIButton *btnIcon;
@property (weak, nonatomic) IBOutlet UIButton *btnNext;
@property (weak, nonatomic) IBOutlet UIView *answersView;
@property (weak, nonatomic) IBOutlet UIView *optionsView;





@property (nonatomic,assign) CGRect iconFrame;//结构体属性,头像按钮的原始大小
@property (weak, nonatomic)  UIButton *cover;//用来引用大图中的阴影

//下一题按钮单击事件
- (IBAction)btnNextClick;
//大图按钮单击事件
- (IBAction)bigImage:(id)sender;
//头像按钮单击事件
- (IBAction)btnIconClick:(id)sender;
- (IBAction)btnTipClick;




@end

@implementation ViewController
//重写方法,设置状态栏为高亮色
-(UIStatusBarStyle)preferredStatusBarStyle{
  return UIStatusBarStyleLightContent;
}
//懒加载数据,重写属性NSArray* questions的get方法
-(NSArray*)questions{
  if (_questions==nil) {
    //加载数据
    NSString* path=[[NSBundle mainBundle]pathForResource:@"questions.plist" ofType:nil];
    NSArray* arraydict=[NSArray arrayWithContentsOfFile:path];
    NSMutableArray* arrayModelM=[NSMutableArray array];
    for (NSDictionary* dict in arraydict) {
      FRQuestion* model=[FRQuestion questionWithDict:dict];
      [arrayModelM addObject:model];
    }
    _questions=arrayModelM;
  }
  return _questions;
}
//隐藏状态栏
-(BOOL)prefersStatusBarHidden{
  return NO;//不隐藏
}


- (void)viewDidLoad {
  [super viewDidLoad];
  //默认初始化显示第一题
  self.index=-1;
  //调用下一题方法
  [self nextQuestion];
}

//点击提示产生的按钮
- (IBAction)btnTipClick {
  //分数-1000
  [self addScore:-1000];
  //把所有的答案按钮”清空“(其实这里清空最好调用每个答案的单击事件)
  for (UIButton* btnAnswer in self.answersView.subviews) {
    //点击一下每个答案按钮
    [self btnAnswerClick:btnAnswer];
  }
  //根据当前的索引。从数据数组中self.questions中找到对应的数据模型,从数据模型中读取正确的答案,把待选按钮中和这个字符相等的按钮点一下
  //先根据问题索引获取模型
  FRQuestion* model=self.questions[self.index];
  //获取相应答案的第一个字符”字符串“
  NSString* firstword=[model.answer substringToIndex:1];
  //根据firstWord在option按钮中找到对应的option按钮,让这个按钮点击一下
  for (UIButton* btnOpt in self.optionsView.subviews) {
    if ([btnOpt.currentTitle isEqualToString:firstword]) {
      //设置某个option按钮点击一下
      [self optionButtonClick:btnOpt];
      break;
    }
  }
}


//点击大图按钮响应的方法

- (IBAction)btnIconClick:(id)sender {
  if (self.cover==nil) {
    [self bigImage:nil];
  }else{
    [self smallImage];
  }
}


//实现大图
- (IBAction)bigImage:(id)sender {
  //0.先记录下头像按钮的原始大小
  self.iconFrame=self.btnIcon.frame;
  
  //1、创建大小与self.view一样的按钮,把这个按钮作为一个阴影
  UIButton* btnCover=[[UIButton alloc]init];
  //设置按钮大小
  btnCover.frame=self.view.bounds;
  btnCover.backgroundColor=[UIColor blackColor];
  btnCover.alpha=0.0;
  //把按钮加到selfview中
  [self.view addSubview:btnCover];
  
  //给阴影注册一个单击事件
  [btnCover addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];
  
  //2、把图片设置到阴影上方
  [self.view bringSubviewToFront:self.btnIcon];//把控件带到view的最上方
  //3、通过动画的方式吧图片变大
  CGFloat icoW=self.view.frame.size.width;
  CGFloat icoH=icoW;
  CGFloat icoX=0;
  CGFloat icoY=(self.view.frame.size.height-icoH)*0.5;
  //设置图片新的frame
  
  [UIView animateWithDuration:1 animations:^{
    btnCover.alpha=0.6;
    
    self.btnIcon.frame=CGRectMake(icoX, icoY, icoW, icoH);
    
  }];
  
  //通过self.cover来应用btncover
  self.cover=btnCover;
  
}

//阴影单击事件方法
-(void)smallImage{
  
  [UIView animateWithDuration:1 animations:^{
    //1、设置头像尺寸还原,
    self.btnIcon.frame=self.iconFrame;
    
    //2.让阴影按钮的透明度变成0
    self.cover.alpha=0.0;
  } completion:^(BOOL finished) {
    //3、移除阴影按钮
    if (finished) {
      //移除阴影按钮
      [self.cover removeFromSuperview];
      //当图像变成小图的时候,再把self.cover设置成nil
      self.cover=nil;
    }

  }];
  

}

//点击访问下一题
- (IBAction)btnNextClick {
  //调用下一题方法
  [self nextQuestion];
}

//显示下一题方法
-(void)nextQuestion{
  //1.让索引++
  self.index++;
  
  //判断当前索引是否越界,如果越界,则提示用户
  if (self.index==self.questions.count) {
    //NSLog(@"答题完毕");
    
    return;
  }
  NSLog(@"%d",self.index);
  //2.获取索引当前模型数据
  FRQuestion*  model=self.questions[self.index];
  //根据模型设置数据
  [self settingData:model];
  
  //4.到达最后一题以后,禁用“下一题”按钮
  if (self.index==self.questions.count-1) {
    _btnNext.enabled=NO;
  }
  //动态创建答案按钮
  [self makeAnswerButtons:model];
  
  //动态生成待选按钮
  [self makeOptionsButton:model];
}


//把模型数据设置到界面上
-(void)settingData:(FRQuestion*) model{
  
  //3.把模型数据设置到界面对应的控件上
  self.lblIndex.text=[NSString stringWithFormat:@"%d/%ld",(self.index+1),self.questions.count];
  self.lbltitle.text=model.title;
  [self.btnIcon setImage:[UIImage imageNamed:model.icon] forState:UIControlStateNormal];
}

//动态创建答案按钮
-(void)makeAnswerButtons:(FRQuestion*)model{
  //清除所有的答案按钮
  /*
  while (self.answersView.subviews.firstObject) {
    [self.answersView.subviews.firstObject removeFromSuperview];
  }*/
  //让subview的每一个对象,分别调用一次removeFromSuperview方法,内部执行了循环,无需我们自己来循环
  [self.answersView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  
  
  //获取当前答案的文字
  NSUInteger len=model.answer.length;
  
  CGFloat margin=10;
  CGFloat btnAnswerW=40;
  CGFloat btnAnswerH=40;
  CGFloat btnAnswerY=0;
  CGFloat marginleft=(self.answersView.frame.size.width-len*btnAnswerW-margin*(len-1))/2;

  //循环创建答案按钮,有几个子就创建几个按钮
  for (int i=0; i<len; i++) {
    //创建一个按钮
    UIButton* btnAnswer=[[UIButton alloc]init];
    [btnAnswer setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //创建frame等
    [btnAnswer setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
    [btnAnswer setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateHighlighted];
    
    CGFloat btnAnswerX=marginleft+i*(btnAnswerW+margin);
    btnAnswer.frame=CGRectMake(btnAnswerX, btnAnswerY, btnAnswerW, btnAnswerH);

    //按钮加到answerView中去
    [self.answersView addSubview:btnAnswer];
    
    //为答案按钮创建单击是见
    [btnAnswer addTarget:self action:@selector(btnAnswerClick:) forControlEvents:UIControlEventTouchUpInside];
  }
  
}

//答案按钮点击事件,参数sender表示当前点击的对象
-(void)btnAnswerClick:(UIButton*)sender{
  //将待选按钮所在的view的交互启用
  self.optionsView.userInteractionEnabled=YES;
  
  //设置每个答案按钮文字为黑色
  [self setAnswerButtonColor:[UIColor blackColor] ];
  
  //找到待选按钮对应的位置,重新设置文字
  for (UIButton* optBtn in self.optionsView.subviews) {
    //判断比较待选按钮文字是否与当前被点击的答案按钮文字一致
    /*
    if ([sender.currentTitle isEqualToString:optBtn.currentTitle]) {
      optBtn.hidden=NO;
      break;
    }
    */
    if(sender.tag==optBtn.tag){
      optBtn.hidden=NO;
      break;
    }
    
  }
  
  //取消按钮文字,设置title为nil
  [sender setTitle:nil forState:UIControlStateNormal];
}
//动态生成待选按钮
-(void)makeOptionsButton:(FRQuestion*)model{
  //设置optionsView可以与用户交互
  [self.optionsView setUserInteractionEnabled:YES];
  //清除所有待选按钮view
  [self.optionsView.subviews
   makeObjectsPerformSelector:@selector(removeFromSuperview)];
  //获取待选按钮的文字的数组
  NSArray* words=model.options;
  
  //指定每个待选按钮的大小
  CGFloat margin=10;
  CGFloat optionW=40;
  CGFloat optionH=optionW;
  
  //指定列数
  int column=7;
  //计算出每行第一个按钮离左边的距离
  CGFloat marginLeft=(self.optionsView.frame.size.width-column*optionW-(column-1)*margin)/2;
  
  //根据待选文字循环创建待选按钮
  for (int i=0; i<words.count; i++) {
    //创建一个按钮并给每个按钮一个唯一的tag值
    UIButton* btnWord=[[UIButton alloc]init];
    btnWord.tag=i;
    //设置按钮背景、文字、frame
    [btnWord setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
    [btnWord setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];

    [btnWord setTitle:words[i] forState:UIControlStateNormal];
    [btnWord setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //计算当前按钮的列的索引和行的索引
    int colIndex=i%column;
    int rowIndex=i/column;
    
    CGFloat optionX=marginLeft+colIndex*(optionW+margin);
    CGFloat optionY=0+rowIndex*(optionH+margin);
    btnWord.frame=CGRectMake(optionX, optionY, optionW, optionH);
    //添加按钮到views上
    [self.optionsView addSubview:btnWord];
    
    //为待选按钮注册单击事件
    [btnWord addTarget:self action:@selector(optionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  }
  
}

//为待选的点击事件
-(void)optionButtonClick:(UIButton*)sender{
  //隐藏当前被点击按钮
  sender.hidden=YES;
  //把当前被点击的按钮的文字显示到第一个为空的“答案按钮上”
  //NSString* text=[sender titleForState:UIControlStateNormal];
  NSString* text=sender.currentTitle;//获取按钮当前状态下的文字
  
  //遍历答案按钮视图上的按钮
  for (UIButton* btnanswer in self.answersView.subviews) {
    if (btnanswer.currentTitle==nil) {
      //给当前点击的按钮的tag值也设置给答案按钮
      btnanswer.tag=sender.tag;
      //把当前点击的待选按钮的文字设置给对应的答案按钮
      [btnanswer setTitle:text forState:UIControlStateNormal];
      break;
    }
  }
  
  //判断答案按钮是否已经填满
  BOOL isFull=YES;
  //声明一个用来保存用户输入答案的字符串
  NSMutableString* userInput=[NSMutableString string];
  for (UIButton* btnanswer in self.answersView.subviews) {
    if (btnanswer.currentTitle==nil) {
      isFull=NO;
      break;
    }else{
      //拿到用户输入的字符串
      [userInput appendString:btnanswer.currentTitle];
    }
  }
  
  if (isFull) {
    //待选按钮的大view禁止交互
    self.optionsView.userInteractionEnabled=NO;
    //如果答案按钮被填满了,那么久判断用户点击输入的答案是否与标准答案一致,如果一致,则设置答案按钮的文字颜色为蓝色并在0.5秒跳转到下一题
    //获取当前题目的正确答案
    FRQuestion* model= self.questions[self.index];
    if ([model.answer isEqualToString:userInput]) {
      //如果正确+100分,直接提取一个方法来计算加减分
      [self addScore:100];
      
      //设置所有的答案按钮的文字颜色为蓝色
      [self setAnswerButtonColor:[UIColor blueColor]];
      //延迟0.5秒后,跳转到下一题
      [self performSelector:@selector(nextQuestion) withObject:nil afterDelay:0.5];
    }else{
      //如果答案不一致(答案错误),则设置答案按钮文字为红色
      [self setAnswerButtonColor:[UIColor redColor]];
    }
  }
  
  
}
//设置答案按钮文字颜色
-(void)setAnswerButtonColor:(UIColor*) color{
  //遍历每个按钮并设置按钮标题文字
  for (UIButton* btnAnswer in self.answersView.subviews) {
    [btnAnswer setTitleColor:color forState:UIControlStateNormal];
  }
}

//一个加减分的方法
-(void)addScore:(int)score{
  //获取按钮上的分分值
  NSString* nowscore= [self.btnScore currentTitle];
  //将分值转换成功数值类型
  int currentScore=nowscore.intValue;
  
  //计算加减
  currentScore+=score;
  //把新的分数设置给按钮
  [self.btnScore setTitle:[NSString stringWithFormat:@"%d",currentScore] forState:UIControlStateNormal];
}

@end
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐