源码下载地址:http://download.youkuaiyun.com/detail/liu537192/8438463
效果图:
核心代码:
//
// MJQuestion.h
// 01-传智猜图
//
// Created by apple on 14-3-27.
// Copyright (c) 2014年 itcast. All rights reserved.
// 题目模型(一个MJQuestion对象对应一道题目)
#import
@interface MJQuestion : NSObject
/**
* 答案
*/
@property (nonatomic, copy) NSString *answer;
/**
* 标题
*/
@property (nonatomic, copy) NSString *title;
/**
* 图标
*/
@property (nonatomic, copy) NSString *icon;
/**
* 待选项
*/
@property (nonatomic, strong) NSArray *options;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)questionWithDict:(NSDictionary *)dict;
@end
//
// MJQuestion.m
// 01-传智猜图
//
// Created by apple on 14-3-27.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "MJQuestion.h"
@implementation MJQuestion
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
self.icon = dict[@"icon"];
self.title = dict[@"title"];
self.answer = dict[@"answer"];
self.options = dict[@"options"];
}
return self;
}
+ (instancetype)questionWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end
//
// MJViewController.m
// 01-传智猜图
//
// Created by apple on 14-3-27.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "MJViewController.h"
#import "MJQuestion.h"
@interface MJViewController ()
//“提示”按钮
- (IBAction)tip;
//“大图”按钮
- (IBAction)bigImg;
//“帮助”按钮
- (IBAction)help;
//“下一题”按钮
- (IBAction)nextQuestion;
//“头像”按钮
- (IBAction)iconClick;
@property (weak, nonatomic) IBOutlet UIButton *scoreBtn;
/** 存放显示答案按钮的view */
@property (weak, nonatomic) IBOutlet UIView *answerView;
@property (weak, nonatomic) IBOutlet UIView *optionView;
/** 图片(头像)序号 */
@property (weak, nonatomic) IBOutlet UILabel *noLabel;
/** 图片(头像)标题 */
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
/** 图片(头像) */
@property (weak, nonatomic) IBOutlet UIButton *iconBtn;
/** “下一题”按钮 */
@property (weak, nonatomic) IBOutlet UIButton *nextQuestionBtn;
/** 遮盖(阴影) */
@property (nonatomic, weak) UIButton *cover;
/** 所有的题目 */
@property (nonatomic, strong) NSArray *questions;
/** 当前是第几题(当前题目的序号) */
@property (nonatomic, assign) int index;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 默认显示index=0对应的题目
self.index = -1;
[self nextQuestion];
}
- (NSArray *)questions
{
if (_questions == nil) {
// 1.加载plist
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"]];
// 2.字典转模型
NSMutableArray *questionArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
MJQuestion *question = [MJQuestion questionWithDict:dict];
[questionArray addObject:question];
}
// 3.赋值
_questions = questionArray;
}
return _questions;
}
/**
* 添加分数
*
* @param deltaScore 需要添加多少分
*/
- (void)addScore:(int)deltaScore
{
int score = [self.scoreBtn titleForState:UIControlStateNormal].intValue;
score += deltaScore;
[self.scoreBtn setTitle:[NSString stringWithFormat:@"%d", score] forState:UIControlStateNormal];
}
/**
* 提示
*/
- (IBAction)tip {
// 1.点击答案区域所有的按钮
for (UIButton *answerBtn in self.answerView.subviews) {
[self answerClick:answerBtn];
}
// 2.取出答案
MJQuestion *question = self.questions[self.index];
// 3.截取答案的第一个字
NSString *firstAnswer = [question.answer substringToIndex:1];
for (UIButton *optionBtn in self.optionView.subviews) {
if ([optionBtn.currentTitle isEqualToString:firstAnswer]) {
[self optionClick:optionBtn];
break;
}
}
// 3.扣分
[self addScore:-1000];
}
/**
* 控制状态栏的样式
*/
- (UIStatusBarStyle)preferredStatusBarStyle
{
// 白色
return UIStatusBarStyleLightContent;
}
/**
* 下一题
*/
- (IBAction)nextQuestion {
// 1.增加索引
self.index++;
// 2.取出模型
MJQuestion *question = self.questions[self.index];
// 3.设置控件的数据
[self settingData:question];
// 4.添加显示答案的按钮
[self addAnswerBtn:question];
// 5.添加待选项按钮
[self addOptionBtn:question];
}
/**
* 设置控件的数据
*/
- (void)settingData:(MJQuestion *)question
{
// 3.1.设置序号
self.noLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + 1, self.questions.count];
// 3.2.设置标题
self.titleLabel.text = question.title;
// 3.3.设置图片
[self.iconBtn setImage:[UIImage imageNamed:question.icon] forState:UIControlStateNormal];
// 3.4.设置下一题按钮的状态
self.nextQuestionBtn.enabled = self.index != (self.questions.count - 1);
}
/**
* 添加待选项按钮
*/
- (void)addOptionBtn:(MJQuestion *)question
{
// 6.1.删掉之前的所有按钮(让数组中的每个对象都执行removeFromSuperview方法)
[self.optionView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
// for (UIView *subview in self.optionView.subviews) {
// [subview removeFromSuperview];
// }
// 6.2.添加新的待选按钮
int count = question.options.count;
for (int i = 0; i