超级猜图简易app代码

  1 //
  2 //  ViewController.m
  3 //  超级猜图
  4 //
  5 //  Created by 刘羽 on 15/12/28.
  6 //  Copyright © 2015年 LX. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #import "Question.h"
 11 //!注意  宏定义后面不要加分号!
 12 #define kButtonWidth 35
 13 #define kButtonHeight 35
 14 #define kButtonMargin 10
 15 #define kTotalCol 7
 16 
 17 
 18 @interface ViewController ()
 19 @property (weak,nonatomic) IBOutlet UIButton *iconButton;
 20 @property (weak, nonatomic) IBOutlet UILabel *noLabel;
 21 @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
 22 @property (weak, nonatomic) IBOutlet UIButton *nextQuestionButton;
 23 @property (weak, nonatomic) IBOutlet UIButton *scoreButton;
 24 @property (weak, nonatomic) IBOutlet UIView *optionsView;
 25 @property (weak, nonatomic) IBOutlet UIView *answerView;
 26 
 27 @property (nonatomic,strong) UIButton *cover;
 28 @property (nonatomic,strong) NSArray *questions;
 29 //设置索引
 30 @property (nonatomic,assign) int index;
 31 @end
 32 
 33 @implementation ViewController
 34 -(void)viewDidLoad
 35 {
 36     [super viewDidLoad];
 37     self.index = -1;
 38     [self nextQuestion];
 39 }
 40 
 41 
 42 //questions懒加载
 43 -(NSArray *)questions
 44 {
 45     if (_questions == nil) {
 46         _questions = [Question questions];
 47     }
 48     return _questions;
 49 }
 50 
 51 //遮罩懒加载
 52 -(UIButton *)cover
 53 {
 54     if (_cover == nil) {
 55         _cover = [[UIButton alloc]initWithFrame:self.view.bounds];
 56         _cover.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
 57         [self.view addSubview:_cover];
 58         _cover.alpha = 0.0;
 59         //此处注意 smallImage方法后面一定要加:  这才是一个完整的方法名
 60         [ _cover addTarget:self action:@selector(bigImage) forControlEvents:UIControlEventTouchUpInside];
 61     }
 62     
 63     return _cover;
 64 }
 65 
 66 //调整状态栏颜色
 67 -(UIStatusBarStyle)preferredStatusBarStyle
 68 {
 69     return  UIStatusBarStyleLightContent;
 70 }
 71 
 72 
 73 
 74 #pragma mark - 大图小图切换
 75 //大图小图切换
 76 -(IBAction)bigImage
 77 {
 78     if(self.cover.alpha == 0.0){
 79     //将图像按钮弄到最前面
 80     [self.view bringSubviewToFront:self.iconButton];
 81     //动画放大图像按钮
 82     CGFloat w = self.view.bounds.size.width;
 83     CGFloat h = w;
 84     CGFloat y = (self.view.bounds.size.height - h) * 0.5;
 85     [UIView animateWithDuration:1.0f animations:^{
 86         self.iconButton.frame = CGRectMake(0, y, w, h);
 87         self.cover.alpha = 1.0;
 88     }];}else{
 89         //切换为小图    动画一旦定义 马上开始
 90         [UIView animateWithDuration:1.0f animations:^{
 91             self.iconButton.frame = CGRectMake(85, 85, 150, 150);
 92             self.cover.alpha = 0.0;
 93         }];
 94         
 95     }
 96     
 97 }
 98 
 99 
100 #pragma mark - 下一题
101 -(IBAction)nextQuestion
102 {
103     //1、拿到当前题目的索引
104     self.index++;
105     //如果index已经到最后一题,提示用户通关。若不处理,程序会闪退(指针越界)
106     if(self.index == self.questions.count){
107         NSLog(@"恭喜通关!");
108         return;
109     }
110     //2、根据索引从数组中取出题目数据模型
111     Question *questions = self.questions[self.index];
112     //3、设置页面上半部分的基本信息区
113     [self setupBasicInfo:questions];
114    
115     //4、设置答案区
116     [self createAnswerButtons:questions];
117     //5、设置备选区
118     [self createOptionsButtons:questions];
119 
120 }
121 //设置页面上半部分的基本信息区
122 -(void)setupBasicInfo:(Question *)questions
123 {
124     self.noLabel.text = [NSString stringWithFormat:@"%d/%d",self.index+1,self.questions.count];
125     self.titleLabel.text = questions.title;
126     [self.iconButton setImage:[UIImage imageNamed:questions.icon] forState:UIControlStateNormal];
127     //如果到达最后一题,禁用下一题按钮
128     self.nextQuestionButton.enabled = (self.index < self.questions.count - 1);
129 }
130 
131 //设置答案区
132 -(void)createAnswerButtons:(Question *)questions
133 {
134     
135     //先清除掉答题区内所有的空间
136     //所有的空间都继承自view,这里用UIview是多态的使用
137     for (UIView *btn in self.answerView.subviews) {
138         [btn removeFromSuperview];
139     }
140     
141     CGFloat answerW = self.answerView.bounds.size.width;
142     int length = questions.answer.length;//计算每一题答案字数(按钮数)
143     //计算第一个按钮的x坐标
144     CGFloat answerX = (answerW - kButtonWidth*length - kButtonMargin*(length - 1))*0.5;
145     //创建每一题答案按钮
146     for (int i = 0; i < length; i++) {
147         CGFloat x = answerX + i * (kButtonMargin + kButtonWidth);
148         UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, kButtonWidth, kButtonHeight)];
149         [btn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
150         [btn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
151         [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
152         [self.answerView addSubview:btn];
153         //这里要注意forControlEvents是UIControlEventTouchUpInside 而不是normal
154         [btn addTarget:self action:@selector(answerClick:) forControlEvents:UIControlEventTouchUpInside];
155     }
156 
157 }
158 
159 
160 //设置备选区
161 -(void)createOptionsButtons:(Question *)questions
162 {
163     
164     if (self.optionsView.subviews.count != questions.options.count) {
165         CGFloat optionW = self.optionsView.bounds.size.width;
166         CGFloat optionX = (optionW - kButtonWidth * kTotalCol - kButtonMargin * (kTotalCol - 1))* 0.5;
167         
168         for (int i = 0; i < questions.options.count; i++) {
169             int row = i / kTotalCol;//
170             int col = i % kTotalCol;//
171             
172             CGFloat x = optionX + col * (kButtonMargin + kButtonWidth);
173             CGFloat y = row * (kButtonHeight + kButtonMargin);
174             
175             UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(x, y , kButtonWidth, kButtonHeight)];
176             [btn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
177             [btn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateNormal];
178             [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
179 
180             [self.optionsView addSubview:btn];
181             //添加监听方法 方法记得加:
182             [btn addTarget:self action:@selector(optionClick:) forControlEvents:UIControlEventTouchUpInside];
183 
184     }
185         
186 
187         }
188     //设置备选区字体:如果按钮已存在,只需要设置标题即可
189     int i= 0;
190     for (UIButton *btn in self.optionsView.subviews) {
191         [btn setTitle:questions.options[i++] forState:UIControlStateNormal];
192         //回复所有按钮的隐藏状态,否则按钮越用越少
193         btn.hidden = NO;
194 
195         }
196     
197 }
198 
199 
200 #pragma mark - 候选按钮点击方法
201 -(void)optionClick:(UIButton *)button
202 {
203     //1、在答案区找到第一个文字为空的按钮
204     UIButton *btn = [self firstAnswerButton];
205     //如果没有找到按钮,直接返回
206     if (btn == nil) return;
207     
208     //2、将button的标题设置给答案区的按钮(即1中的返回值)
209     [btn setTitle:button.currentTitle forState:UIControlStateNormal];
210     
211     //3、将button隐藏
212     button.hidden = YES;
213     
214     //4、判断结果
215     [self judge];
216 }
217 //判断结果
218 -(void)judge
219 {
220     //如果四个按钮都有文字,才需要判断结果
221     //遍历所有答案区的按钮
222     BOOL isFull = YES;
223     NSMutableString *strM = [NSMutableString string];
224     
225     for (UIButton *btn in self.answerView.subviews) {
226         if (btn.currentTitle.length == 0) {
227             //只要有一个按钮没有字
228             isFull = NO;
229             break;
230         }else
231         {
232             //如果按钮都有字了,则拼接字符串,准备判断
233             [strM appendString:btn.currentTitle];
234         }
235     }
236     
237     if (isFull) {
238         //根据self.index获得当前的question答案
239         Question *questions = self.questions[self.index];
240         //如果一致 进入下一题
241         if ([strM isEqualToString:questions.answer]) {
242             [self setAnswerButtonColor:[UIColor blueColor]];
243             //加分
244             [self changeScore:800];
245             //等待0.5s 进入下一题
246             [self performSelector:@selector(nextQuestion) withObject:nil afterDelay:0.5];
247         }else
248         {
249             //如果不一致,修改按钮颜色提示用户
250             [self setAnswerButtonColor:[UIColor redColor]];
251         }
252     }
253     
254 }
255 
256 
257 //修改答案区按钮颜色
258 -(void)setAnswerButtonColor:(UIColor *)color
259 {
260     for (UIButton *btn in self.answerView.subviews) {
261         [btn setTitleColor:color forState:UIControlStateNormal];
262     }
263 }
264 
265 
266  //在答案区找到第一个文字为空的按钮
267 -(UIButton *)firstAnswerButton
268 {
269     //遍历答案区的所有按钮
270     for (UIButton *btn in self.answerView.subviews) {
271         if (btn.currentTitle.length == 0) {
272             return btn;
273         }
274     }
275     return nil;
276 
277 }
278 
279 #pragma mark - 设置答案区按钮(button:答案区按钮对象  btn:候选区按钮对象)
280 -(void)answerClick:(UIButton *)button
281 {
282     //如果按钮没有字直接返回
283     if(button.currentTitle.length == 0) return;
284     //遍历候选区按钮并找到对应的隐藏的字
285     UIButton *btn = [self optionButtonWithTitle:button.currentTitle isHidden:YES];
286     //显示对应的按钮
287     btn.hidden = NO;
288     //清除button的文字
289     [button setTitle:@"" forState:UIControlStateNormal];
290     //只要点击了答题区文字,则答题区文字不完整,将颜色改为黑色
291     [self setAnswerButtonColor:[UIColor blackColor]];
292 }
293 //遍历候选区按钮并找到对应的隐藏的字
294 
295 -(UIButton *)optionButtonWithTitle:(NSString *)title isHidden:(BOOL)isHidden
296 {
297     //遍历候选区中所有的按钮
298     for (UIButton *btn in self.optionsView.subviews) {
299         if ([btn.currentTitle isEqualToString:title] && btn.hidden == isHidden) {
300             return btn;
301         }
302         
303     }
304     return nil;
305 }
306 #pragma mark - 提示功能
307 -(IBAction)tipClick
308 {
309     //1、把答题区中所有的按钮清空
310     for (UIButton *btn in self.answerView.subviews) {
311         //调用之前创建的点击答案的方法 清空答题区
312         [self answerClick:btn];
313         
314     }
315     //把正确答案的第一个字设置到答题区中
316     //1、获取答案的第一个字
317     Question *questions = self.questions[self.index];
318     //截取第一个字符
319     NSString *first = [questions.answer substringToIndex:1];
320     //取出文字对应的候选按钮
321 //    for(UIButton *btn in self.optionsView.subviews){
322 //        if ([btn.currentTitle isEqualToString:first] && !btn.hidden ) {
323 //            //调用候选区按钮点击方法,设置第一个字
324 //            [self optionClick:btn];
325 //        }
326     //可以直接使用之前定义过的方法查找候选按钮
327     UIButton *btn = [self optionButtonWithTitle:first isHidden:NO];
328     //直接调用候选区点击方法
329     [self optionClick:btn];
330     //扣分
331     [self changeScore:-1000];
332     }
333 
334 
335 
336 #pragma mark - 分数处理
337 -(void)changeScore:(int)score
338 {
339     //取出当前分数
340     int currentScore = self.scoreButton.currentTitle.intValue;
341     //使用score调整分数
342     currentScore += score;
343     //重新设置分数
344     [self.scoreButton setTitle:[NSString stringWithFormat:@"%d",currentScore] forState:UIControlStateNormal];
345 }
346 @end
 1 //
 2 //  Question.h
 3 //  超级猜图
 4 //
 5 //  Created by 刘羽 on 15/12/29.
 6 //  Copyright © 2015年 LX. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface Question : NSObject
12 @property(nonatomic,strong) NSString *answer;
13 @property(nonatomic,strong) NSString *icon;
14 @property(nonatomic,strong) NSString *title;
15 @property(nonatomic,strong) NSArray *options;
16 
17 -(instancetype)initWithDict:(NSDictionary *)dict;
18 +(instancetype)questionWithDict:(NSDictionary *)dict;
19 +(NSArray *)questions;
20 @end
 1 //
 2 //  Question.m
 3 //  超级猜图
 4 //
 5 //  Created by 刘羽 on 15/12/29.
 6 //  Copyright © 2015年 LX. All rights reserved.
 7 //
 8 
 9 #import "Question.h"
10 
11 @implementation Question
12 -(instancetype)initWithDict:(NSDictionary *)dict
13 {
14     self = [super self];
15     if (self) {
16         [self setValuesForKeysWithDictionary:dict];
17         
18     }
19     return self;
20 }
21 
22 +(instancetype)questionWithDict:(NSDictionary *)dict
23 {
24     return  [[self alloc] initWithDict:dict];
25 }
26 
27 +(NSArray *)questions
28 {
29     NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil]];
30     NSMutableArray *arrayM = [NSMutableArray array ];
31     for (NSDictionary *dict in array) {
32         [arrayM addObject:[self questionWithDict:dict]];
33     }
34     return arrayM;
35 }
36 @end

 

posted on 2015-12-31 16:15  诗酒趁华年  阅读(492)  评论(0)    收藏  举报

导航