猜图
//
// ViewController.m
// ios20141227s1
//
// Created by fulin on 14/12/27.
// Copyright (c) 2014年 fulin. All rights reserved.
//
#import "ViewController.h"
#import "Question.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *noLable;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *iconBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextQuestionBtn;
@property (weak, nonatomic) IBOutlet UIButton *cover;
@property (weak, nonatomic) IBOutlet UIView *answerView;//存放正确答案的View
@property (weak, nonatomic) IBOutlet UIView *optionView;
- (IBAction)tip;
- (IBAction)bigImg;
- (IBAction)help;
- (IBAction)nextQuestion;
- (IBAction)iconClick;
@property (nonatomic, assign)int index;//记录当前题目的序号(索引)
@property (nonatomic, strong) NSArray *questions;//存放所有题目
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化,这里很不错的技巧,初始值为-1
self.index = -1;
[self nextQuestion];
}
- (NSArray *)questions
{
if (_questions == nil) {
//1、加载plist
NSArray *dictAry = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil]];
//2、字典转模型
NSMutableArray *questionArray = [NSMutableArray array];
for (NSDictionary *dict in dictAry) {
Question *question = [Question questionWithDict:dict];
[questionArray addObject:question];
}
//3、赋值
_questions = questionArray;
}
return _questions;
}
/**
控制状态栏样式
*/
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
/**
* 下一题
*/
- (IBAction)nextQuestion
{
//1、增加索引
self.index ++;
//2、取出模型
Question *question = self.questions[self.index];
//3、设置数据
[self.noLable setText:[NSString stringWithFormat:@"%d/%lu", self.index + 1, (unsigned long)self.questions.count]];
[self.titleLabel setText:question.title];
[self.iconBtn setImage:[UIImage imageNamed:question.icon] forState:UIControlStateNormal];
//4、设置下一题按钮状态
self.nextQuestionBtn.enabled = self.index != self.questions.count -1;
//5、添加正确答案
[self addAnswerBtn:question];
//6、添加待选项
[self addOptionsBtn:question];
}
/**
* 添加正确答案
*/
- (void)addAnswerBtn: (Question *)question
{
//5.1、清除所有子View
for (UIView *views in self.answerView.subviews) {
[views removeFromSuperview];
}
//5.2、添加子View
int length = (int)question.answer.length;
for(int i = 0 ; i < length; i++)
{
UIButton *answerBtn = [[UIButton alloc]init];
[answerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[answerBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
[answerBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
CGFloat margin = 10;//中间间距
CGFloat answerW = 35;
CGFloat answerH = 35;
CGFloat lenthMargin = (self.view.frame.size.width - length * answerW - (length - 1) * margin) * 0.5;//最左边的间距
CGFloat answerX = lenthMargin + i * (answerW + margin);
answerBtn.frame = CGRectMake(answerX, 0, answerW, answerH);
[self.answerView addSubview:answerBtn];
//答案按钮添加监听器
[answerBtn addTarget:self action:@selector(answerClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
/**
* 监听答案按钮
*/
- (void) answerClick:(UIButton *)answerBtn
{
//1、让答案的待选按钮文字显示
//答案按钮文字
NSString *answerTitleValue = [answerBtn titleForState:UIControlStateNormal];
for (UIButton *optionBtn in self.optionView.subviews) {
//待选按钮文字
NSString *optinTitleValue = [optionBtn titleForState:UIControlStateNormal];
if ([answerTitleValue isEqualToString:optinTitleValue]) {
[optionBtn setHidden:NO];
break;
}
}
//2、被点击答案文字消失
[answerBtn setTitle:nil forState:UIControlStateNormal];
}
/**
添加待选项
*/
- (void)addOptionsBtn:(Question *)question
{
//6.1删除前面的按钮
for (UIView *view in self.optionView.subviews) {
[view removeFromSuperview];
}
//6.2添加新按钮
int count = (int)question.options.count;
for (int i = 0; i < count; i++) {
UIButton *optionBtn = [[UIButton alloc]init];
[optionBtn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
[optionBtn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
CGFloat optionW = 35;
CGFloat optionH = 35;
CGFloat margin = 10;
int totalColumn = 6;
CGFloat leftMargin = (self.view.frame.size.width - totalColumn * optionW - (totalColumn - 1) * margin) * 0.5;
CGFloat col = i % totalColumn;
CGFloat row = i / totalColumn;
CGFloat optionX = leftMargin + col * (optionW + margin);
CGFloat optionY = row * (optionH + margin);
optionBtn.frame = CGRectMake(optionX, optionY, optionW, optionH);
[optionBtn setTitle:question.options[i] forState:UIControlStateNormal];
[optionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.optionView addSubview:optionBtn];
//添加监听器
[optionBtn addTarget:self action:@selector(optionClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
/**
* 用户点击待选项
*/
- (void)optionClick:(UIButton *)optionBtn
{
//1、隐藏当前视图
[optionBtn setHidden:YES];//当前隐藏
//2、显示选中文字
BOOL canCalculationResult = YES;
NSMutableString *tempAnswerTitle = [NSMutableString string];
for(UIButton *answerBtn in self.answerView.subviews)
{
//判断按钮是否有文字
NSString *answerTitle = [answerBtn titleForState:UIControlStateNormal];//取文字
if (answerTitle.length == 0) {
//设置文字
NSString *optionTitle = [optionBtn titleForState:UIControlStateNormal];
[answerBtn setTitle:optionTitle forState:UIControlStateNormal];
break;
}
}
for(UIButton *answerBtn in self.answerView.subviews)
{
//判断按钮是否有文字
NSString *answerTitle = [answerBtn titleForState:UIControlStateNormal];//取文字
if (answerTitle.length == 0) {
canCalculationResult = NO;
}
//拼接按钮文字
if(answerTitle){
[tempAnswerTitle appendString:answerTitle];
}
}
//判断用户输入是否正确
if (canCalculationResult) {
Question *question = self.questions[self.index];
if([tempAnswerTitle isEqualToString:question.answer])
{
[self performSelector:@selector(nextQuestion) withObject:nil afterDelay:0.5];
}
}
}
/**
* 点击中间图像调用方法
*/
- (IBAction)iconClick
{
if (self.cover == nil)
{
[self bigImg];
}
else
{
[self smallImg];
}
}
/**
* 大图
*/
- (IBAction)bigImg
{
//1、添加阴影
UIButton *cover = [[UIButton alloc] init];
cover.frame = self.view.bounds;//重点,全屏的frame就是self.view.bounds
cover.backgroundColor = [UIColor blackColor];
cover.alpha = 0.0;
//给遮盖的视图添加监听器
[cover addTarget:self action:@selector(smallImg) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cover];
self.cover = cover;
//2、更换阴影和头像的位置
[self.view bringSubviewToFront:self.iconBtn];//把某个控件带到最前面
//3、更改头像的Frame
CGFloat iconW = self.view.frame.size.width;
CGFloat iconH = iconW;
CGFloat iconY = (self.view.frame.size.height - iconH) / 2;
[UIView animateWithDuration:1.0 animations:^{
cover.alpha = 0.7;
self.iconBtn.frame = CGRectMake(0, iconY, iconW, iconH);
}];
}
/**
* 小图
*/
- (void) smallImg
{
//第一种执行动画的方案
if(false){
//删除阴影
//面向对象,任何视图想从主视图中删除,都调用自己的方法,“只有自己才知道自己怎么死的”
//恢复图像原来位置
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
self.cover.alpha = 0.0;
self.iconBtn.frame = CGRectMake(85, 85, 150, 150);
[UIView commitAnimations];
//[self.cover removeFromSuperview];
//self.cover = nil;
}
//第二种执行动画的方案,推荐
[UIView animateWithDuration:1.0 animations:^{
self.cover.alpha = 0.0;
self.iconBtn.frame = CGRectMake(85, 85, 150, 150);
} completion:^(BOOL finished) {
[self.cover removeFromSuperview];
self.cover = nil;
}];
}
/**
* 提示按钮
*/
- (IBAction)tip {
}
/**
* 帮助
*/
- (IBAction)help {
}
@end
猜图项目全部代码
最新推荐文章于 2023-08-26 00:52:03 发布