#import "HMViewController.h"
#import "HMQuestion.h"
#define kButtonWidth 35
#define kButtonHeight 35
#define kButtonMargin 10
#define kTotolCol 7
@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIButton *iconButton;
@property (weak, nonatomic) IBOutlet UILabel *noLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *nextQuestionButton;
@property (weak, nonatomic) IBOutlet UIView *answerView;//答案框
@property (weak, nonatomic) IBOutlet UIView *optionsView;//选择框
@property (nonatomic, strong) NSArray *questions; //数据
/** 题目索引 */
@property (nonatomic, assign) int index;
@end
@implementation HMViewController
- (NSArray *)questions
{
if (_questions == nil) {
_questions = [HMQuestion questions];
}
return _questions;
}
- (UIButton *)cover
{
if (_cover == nil) {
_cover = [[UIButton alloc] initWithFrame:self.view.bounds];
_cover.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
[self.view addSubview:_cover];
_cover.alpha = 0.0;
[_cover addTarget:self action:@selector(bigImage) forControlEvents:UIControlEventTouchUpInside];
}
return _cover;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.index = -;
[self nextQuestion];
}
/** 调整状态栏颜色 */
/**
UIStatusBarStyleDefault 黑色状态栏
UIStatusBarStyleLightContent 亮色状态栏
*/
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
#pragma mark - 下一题
- (IBAction)nextQuestion
{
// 1. 当前答题的索引,索引递增
self.index++;
// 2. 从数组中按照索引取出题目模型数据
HMQuestion *question = self.questions[self.index];
// 3. 设置基本信息
self.noLabel.text = [NSString stringWithFormat:@"%d/%d", self.index + , self.questions.count];
self.titleLabel.text = question.title;
[self.iconButton setImage:[UIImage imageNamed:question.icon] forState:UIControlStateNormal];
// 如果到达最后一题,禁用下一题按钮
self.nextQuestionButton.enabled = (self.index < self.questions.count - );
// 4. 设置答案按钮
// 首先清除掉答题区内的所有按钮
// 所有的控件都继承自UIView,多态的应用
for (UIView *btn in self.answerView.subviews) {
[btn removeFromSuperview];
}
CGFloat answerW = self.answerView.bounds.size.width;
int length = question.answer.length;
CGFloat answerX = (answerW - kButtonWidth * length - kButtonMargin * (length - )) * 0.5;
// 创建所有答案的按钮
for (int i = ; i < length; i++) {
CGFloat x = answerX + i * (kButtonMargin + kButtonWidth);
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, , kButtonWidth, kButtonHeight)];
[btn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
[self.answerView addSubview:btn];
}
// 5. 设置备选按钮
for (UIView *view in self.optionsView.subviews) {
[view removeFromSuperview];
}
CGFloat optionW = self.optionsView.bounds.size.width;
CGFloat optionX = (optionW - kTotolCol * kButtonWidth - (kTotolCol - ) * kButtonMargin) * 0.5;
for (int i = ; i < question.options.count; i++) {
int row = i / kTotolCol;
int col = i % kTotolCol;
CGFloat x = optionX + col * (kButtonMargin + kButtonWidth);
CGFloat y = row * (kButtonMargin + kButtonHeight);
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, kButtonWidth, kButtonHeight)];
[btn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
// 设置备选答案
[btn setTitle:question.options[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.optionsView addSubview:btn];
}
NSLog(@"%d", self.optionsView.subviews.count);
}
@end