#import "ViewController.h" #import "JWQuestion.h" #import "NSArray+Log.h" #define kMargin 13 @interfaceViewController () // 1/10 @property (weak, nonatomic) IBOutletUILabel *currenNumLabel; // 喜剧大片 @property (weak, nonatomic) IBOutletUILabel *titleLabel; // 专门管理答案区域的Button @property (weak, nonatomic) IBOutletUIView *answerView; @property (weak, nonatomic) IBOutletUIView *optionView; @property (weak, nonatomic) IBOutletUIButton *iconBtn; // 灰灰的遮盖 @property (strong, nonatomic) UIButton *cover; @property (strong, nonatomic) NSArray * questionDatas; @property(assign, nonatomic) int index; @property (weak, nonatomic) IBOutletUIButton *nextQuestionBtn; @end @implementation ViewController -(NSArray *)questionDatas { if (!_questionDatas) { // NSArray *allDatas = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"]]; // // NSMutableArray *tempArr = [NSMutableArray array]; // // for (NSDictionary *dict in allDatas) { // // JWQuestion *question = [JWQuestion questionWithDict:dict]; // [tempArr addObject:question]; // } // // 放JWQuestion对象 _questionDatas = [JWQuestionquestion]; } return_questionDatas; } -(UIButton *)cover { if (!_cover) { // 创建一个半透明的Button _cover = [[UIButtonalloc]initWithFrame:self.view.bounds]; _cover.backgroundColor = [UIColorblackColor]; _cover.alpha = 0.0;
[_coveraddTarget:selfaction:@selector(bigImage:) forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:_cover]; self.cover = _cover; } return_cover; } - (void)viewDidLoad { [superviewDidLoad];
self.index--; // 进来就加载数据 [selfnextQuestion:nil]; } // 点击“大图” - (IBAction)bigImage:(id)sender { //创建半透明的Button [selfcover];
// 图片放到最上层
[self.viewbringSubviewToFront:self.iconBtn];
// 计算图片改变后的大小 CGFloat iconBtnW = self.view.bounds.size.width; CGFloat iconBtnH = iconBtnW; CGFloat iconBtnY = (self.view.bounds.size.height - iconBtnW) * 0.5;
// 判断现在是不是大图是大图就变小 if (0.0 == self.cover.alpha) { // 小图 // 动画放大 [UIViewanimateWithDuration:1.0 animations:^{
self.cover.alpha = 0.5;
self.iconBtn.frame = CGRectMake(0, iconBtnY, iconBtnW, iconBtnH); }]; } else // 已经是大图 { [UIViewanimateWithDuration:1.0 animations:^{
self.iconBtn.frame = CGRectMake(107 , 108, 160, 160);
// alpha < 0.001的时候,不响应事件 self.cover.alpha = 0.0; }]; } } // 点击“下一题” - (IBAction)nextQuestion:(UIButton *)sender {
self.index ++;
// 取出数据 JWQuestion *question = self.questionDatas[self.index]; // 专门切换所有数据 [selfchangeData:question];
// 创建answer区域的按钮之前清空上次创建的按钮 [selfcreatAnswerBtnWithQuestion:question]; // 创建option区域的按钮 [selfcreatOptionBtnWithQuestion:question]; } // 专门切换所有数据 -(void)changeData:(JWQuestion *)question { self.currenNumLabel.text = [NSStringstringWithFormat:@"%d/%ld",self.index + 1,self.questionDatas.count]; self.titleLabel.text = question.title;
[self.iconBtnsetImage:[UIImageimageNamed:question.icon] forState:UIControlStateNormal];
// 3> 不能点击 self.nextQuestionBtn.enabled = self.index != self.questionDatas.count-1; } // 创建answer区域的按钮之前清空上次创建的按钮 -(void)creatAnswerBtnWithQuestion:(JWQuestion *)question { for (UIButton *answerBtn inself.answerView.subviews) { [answerBtn removeFromSuperview]; } // 拿出答案 NSUInteger answerbtnCount = question.answer.length;
CGFloat answerBtnW = 35; CGFloat answerBtnH = answerBtnW;
// 不论几个按钮,都在屏幕中间;左边和右边间距一样
CGFloat answerBtnStartX = (self.view.bounds.size.width - answerbtnCount * answerBtnW - (answerbtnCount - 1) * kMargin) * 0.5;
for (int i = 0 ; i < answerbtnCount; i ++) {
CGFloat answerBtnX = answerBtnStartX + (answerBtnW + kMargin) * i;
UIButton *answerBtn = [[UIButtonalloc]initWithFrame:CGRectMake(answerBtnX, 0, answerBtnW, answerBtnH)]; [answerBtn setBackgroundImage:[UIImageimageNamed:@"btn_answer"] forState:UIControlStateNormal]; [answerBtn setBackgroundImage:[UIImageimageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
[self.answerViewaddSubview:answerBtn]; } } // 创建option区域的按钮 -(void)creatOptionBtnWithQuestion:(JWQuestion *)question { // 个数== options数组的count 左边和右边间距一样
for (UIButton *optionBtn inself.optionView.subviews) { [optionBtn removeFromSuperview]; }
NSUInteger optionBtnCount = question.options.count;
CGFloat optionBtnW = 40; CGFloat optionBtnH = optionBtnW;
int totolCol = 7;
CGFloat optionBtnStartX = (self.view.bounds.size.width - totolCol *optionBtnW - (totolCol - 1) *kMargin) * 0.5;
for (int i = 0 ; i < optionBtnCount; i ++) {
int row = i / totolCol; int col = i % totolCol;
CGFloat optionBtnX = optionBtnStartX + (optionBtnW + kMargin) *col; CGFloat optionBtnY = (optionBtnH + kMargin) * row;
UIButton *optionBtn = [[UIButtonalloc]initWithFrame:CGRectMake(optionBtnX,optionBtnY , optionBtnW, optionBtnH)];
[optionBtn setTitle:question.options[i] forState:UIControlStateNormal]; [optionBtn setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
[optionBtn setBackgroundImage:[UIImageimageNamed:@"btn_option"] forState:UIControlStateNormal]; [optionBtn setBackgroundImage:[UIImageimageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
[optionBtn addTarget:selfaction:@selector(optionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.optionViewaddSubview:optionBtn]; } } #warning 讲到这里了----------- -(void)optionBtnClick:(UIButton *)optionBtn { // 找到answer区域第一个没有title的btn
// 把option隐藏把title赋值给answerBtn的title
optionBtn.hidden = YES;
} @end |