iOS -- 传智猜图

/* Question.h */

#import <Foundation/Foundation.h>
@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

/* Question.m */

#import "Question.h"
@implementation Question
- (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

/* ViewController.m */

#import "ViewController.h"
#import "Question.h"
@interface ViewController ()
- (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 ViewController
- (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) {
            Question *question = [Question 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.取出答案
    Question *question = self.questions[self.index];
    // 答案的第一个文字
    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.取出模型
    Question *question = self.questions[self.index];
    // 3.设置控件的数据
    [self settingData:question];
    // 4.添加正确答案
    [self addAnswerBtn:question];
    // 5.添加待选项
    [self addOptionBtn:question];
}
// 设置控件的数据
 - (void)settingData:(Question *)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:(Question *)question{
    // 6.1.删掉之前的所有按钮
    [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<count; i++) {
        // 6.2.1.创建按钮
        UIButton *optionBtn = [[UIButton alloc] init];
        // 6.2.2.设置背景
        [optionBtn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
        [optionBtn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
        // 6.2.3.设置frame
        // 按钮尺寸
        CGFloat optionW = 35;
        CGFloat optionH = 35;
        // 按钮之间的间距
        CGFloat margin = 10;
        // 控制器view的宽度
        CGFloat viewW = self.view.frame.size.width;
        // 总列数
        int totalColumns = 7;
        // 最左边的间距 = 0.5 * (控制器view的宽度 - 总列数 * 按钮宽度 - (总列数 - 1) * 按钮之间的间距)
        CGFloat leftMargin = (viewW - totalColumns * optionW - margin * (totalColumns - 1)) * 0.5;
        int col = i % totalColumns;
        // 按钮的x = 最左边的间距 + 列号 * (按钮宽度 + 按钮之间的间距)
        CGFloat optionX = leftMargin + col * (optionW + margin);
        int row = i / totalColumns;
        // 按钮的y = 行号 * (按钮高度 + 按钮之间的间距)
        CGFloat optionY = row * (optionH + margin);
        optionBtn.frame = CGRectMake(optionX, optionY, optionW, optionH);
        // 6.2.4.设置文字
        [optionBtn setTitle:question.options[i] forState:UIControlStateNormal];
        [optionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        // 6.2.5.添加
        [self.optionView addSubview:optionBtn];
        // 6.2.6.监听点击
        [optionBtn addTarget:self action:@selector(optionClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}
// 监听待选按钮的点击
 - (void)optionClick:(UIButton *)optionBtn{
    // 1.让被点击的待选按钮消失
    optionBtn.hidden = YES;
    // 2.显示文字到正确答案上
    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; // 停止遍历
        }
    }
    // 3.检测答案是否填满
    BOOL full = YES;
    NSMutableString *tempAnswerTitle = [NSMutableString string];
    for (UIButton *answerBtn in self.answerView.subviews) {
        // 判断按钮是否有文字
        NSString *answerTitle = [answerBtn titleForState:UIControlStateNormal];
        if (answerTitle.length == 0) { // 没有文字(按钮没有填满)
            full = NO;
        }
        // 拼接按钮文字
        if(answerTitle) {
            [tempAnswerTitle appendString:answerTitle];
        }
    }
    // 4.答案满了
    if (full) {
        Question *question = self.questions[self.index];
        if ([tempAnswerTitle isEqualToString:question.answer]) { // 答对了(文字显示蓝色)
            for (UIButton *answerBtn in self.answerView.subviews) {
                [answerBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            }
            // 加分
            [self addScore:800];
            // 0.5秒后跳到下一题
            [self performSelector:@selector(nextQuestion) withObject:nil afterDelay:0.5];
        } else { // 答错了(文字显示红色)
            for (UIButton *answerBtn in self.answerView.subviews) {
                [answerBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            }
        }
    }
}
// 添加正确答案
- (void)addAnswerBtn:(Question *)question{
    // 5.1.删除之前的所有按钮
    // 让数组中的所有对象都执行removeFromSuperview方法
    [self.answerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    for (UIView *subview in self.answerView.subviews) {
//        [subview removeFromSuperview];
//    }
    // 5.2.添加新的答案按钮
    int length = question.answer.length;
    for (int i = 0; i<length; i++) {
        // 5.2.1.创建按钮
        UIButton *answerBtn = [[UIButton alloc] init];
        [answerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        // 5.2.2.设置背景
        [answerBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
        [answerBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
        // 5.2.3.设置frame
        CGFloat viewW = self.view.frame.size.width;
        // 按钮之间的间距
        CGFloat margin = 10;
        // 按钮的尺寸
        CGFloat answerW = 35;
        CGFloat answerH = 35;
        // 最左边的间距 = 0.5 * (控制器view的宽度 - 按钮个数 * 按钮宽度 - (按钮个数 - 1) * 按钮之间的间距)
        CGFloat leftMargin = (viewW - length * answerW - margin * (length - 1)) * 0.5;
        // 按钮的x = 最左边的间距 + i * (按钮宽度 + 按钮之间的间距)
        CGFloat answerX = leftMargin + i * (answerW + margin);
        answerBtn.frame = CGRectMake(answerX, 0, answerW, answerH);
        // 5.2.4.添加
        [self.answerView addSubview:answerBtn];
        // 5.2.5.监听点击
        [answerBtn addTarget:self action:@selector(answerClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}
// 监听答案按钮的点击
- (void)answerClick:(UIButton *)answerBtn{
    // 1.让答案按钮文字对应的待选按钮显示出来(hidden = NO)
    for (UIButton *optionBtn in self.optionView.subviews) {
        if ([optionBtn.currentTitle isEqualToString:answerBtn.currentTitle]
        && optionBtn.hidden == YES) { // 发现跟答案按钮相同文字的待选按钮
            optionBtn.hidden = NO;
            break;
        }
    }
    // 2.让被点击答案按钮的文字消失(去除文字)
    [answerBtn setTitle:nil forState:UIControlStateNormal];
    // 3.让所有的答案按钮变为黑色
    for (UIButton *answerBtn in self.answerView.subviews) {
        [answerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
}
 // 点击头像
- (IBAction)iconClick {
    if (self.cover == nil) { // 没有遮盖,要放大
        [self bigImg];
    } else { // 有遮盖,要缩小
        [self smallImg];
    }
}
- (IBAction)bigImg {
    // 1.添加阴影
    UIButton *cover = [[UIButton alloc] init];
    cover.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.执行动画
    [UIView animateWithDuration:0.25 animations:^{
        // 3.1.阴影慢慢显示出来
        cover.alpha = 0.7;
        // 3.2.头像慢慢变大,慢慢移动到屏幕的中间
        CGFloat iconW = self.view.frame.size.width;
        CGFloat iconH = iconW;
        CGFloat iconY = (self.view.frame.size.height - iconH) * 0.5;
        self.iconBtn.frame = CGRectMake(0, iconY, iconW, iconH);
    }];
}
- (void)smallImg{
    // 执行动画
    [UIView animateWithDuration:0.25 animations:^{
        // 存放需要执行动画的代码
        // 1.头像慢慢变为原来的位置和尺寸
        self.iconBtn.frame = CGRectMake(85, 80, 150, 150);
        // 2.阴影慢慢消失
        self.cover.alpha = 0.0;
    } completion:^(BOOL finished) {
        // 动画执行完毕后会自动调用这个block内部的代码
        // 3.动画执行完毕后,移除遮盖(从内存中移除)
        [self.cover removeFromSuperview];
        self.cover = nil;
    }];
}
@end

时间: 2024-08-09 14:46:54

iOS -- 传智猜图的相关文章

传智播客iOS培训:做被争抢的iOS开发者

学iOS有前途吗?iOS培训有前途吗?就业前景如何?iOS开发工程师已经饱和了吗? 最近有很多想来学习iOS开发的学员,提出了一系列对iOS的质疑.作为一家专门致力于为广大学员提供更前沿.更牛的IT技术培训机构,传智播客很高兴.很欣慰能听到大家的真实心声,并得到大家的宝贵建议.面对大家提出的质疑和内心的困惑,我们不逃避.不掩饰,愿用一颗坦诚之心去解答学员们提出的质疑!  iOS是目前全球最为流行的操作系统之一,四大就业优势让你无从拒绝! 优势一:开发环境好.苹果的开发环境是Xcode,具有运行速

DIV + CSS综合实例【传智PHP首页】

1.首页结构 2.准备工作 所有素材放到与当前网页同级的目录下: 网页背景色.背景图: 主页宽度:1000px: 创建CSS文件,将CSS文件引入到当前的HTML文件中. 3.实现 效果图: 源代码: HTML代码: 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&

传智播客C/C++学员荣膺微软&amp;Cocos 2d-x黑客松最佳创新奖

?? 6月30日,历时32小时的微软开放技术Cocos 2d-x 编程黑客松在北京望京微软大厦成功落下帷幕,这是微软开放技术首次联合Cocos 2d-x 在中国举办黑客松.此次活动共有包括传智播客C/C++学院的学员组成的闪游队,以及Hurry队.繁星队等在内的18个团队,70人参加了这场开发盛事,参赛团队中,不乏经验丰富的一线手游团队. "太空大战"项目演示 微软开放技术Cocos 2d-x 编程黑客松,是微软开放技术.Cocos2d-x 开源引擎联合举办的一场属于游戏编程开发者的聚

传智官网

css代: body,ul,li,p,img,h1,h2,h3,h4,h5,h6{margin:0;padding:0;} body{ font-family:"微软雅黑"; font-size:12px; color:#444; background:url(images/bg-body.gif) repeat-x; } .box{ width:973px; margin:0px auto; } .header{ height:196px; } .header .top .left1

传智播客内部 学习网站+书籍分享

IT社区: http://www.cnblogs.com/ http://www.csdn.net/ http://www.51cto.com/ http://www.cocoachina.com/ http://www.techcn.com.cn/ http://mobilehub.io/   推荐 iOS 学习网站: http://www.w3school.com.cn/ http://objccn.io/ http://github.ibireme.com/github/list/ios/

传智学习的目的,以及对未来人生的规划

首先介绍一下自己,本人本科,通信工程,毕业之后找了份无线网络优化的工作,刚找到工作时感觉这份工作应该是不错的,工作之后才发现这份工作太辛苦了,白天爬山勘站还有出调整方案,到晚上写报告,每天早上八点开始工作,一直忙到晚上十点十一点.没有加班工费,每个月工资只有2k. 有同学已经做了码农,工作比我轻松,工资比我高,遂决定  辞职,转行! 辞职后开始纠结了,该学什么好?本来想学安卓或者iOS开发的,同学说学这两个的人太多,现在已经不好找工作了,Java和PHP还可以,遂决定来传智学PHP. IT行业是

如何写好一个网页---传智首页

---恢复内容开始--- 学习前端知识也有一个月了吧,十月十四号来到这里学习的,一直到11月7号,差不多也有一个月的时间了,所以今天想谢谢这个时间段学到的一些知识,也留着以后能够回头看看这段时间的付出. 今天写的主题内容是一个简单的网页制作,用的模板的话就是传智的首页,因为自己素材只有传智首页的素材罢了... 对我而言,想要去设计一个网站,我先会做大量的分析,第一步做的就是对整个网站的整体框架的和模块的划分.就例如传智首页来说,我选择将这个首页看成是一个最大的整体模块,然后在这个模块里面去再划分

传智播客C语言视频第二季(增加诸多C语言案例讲解,有效下载期为10.5-10.10关闭)

?? 卷 backup 的文件夹 PATH 列表卷序列号为 000000F4 D4A8:14B0J:.│  1.txt│  2.txt│  ├─1传智播客_尹成_C语言从菜鸟到高手_第一章C语言概述A│  ├─文档│  │      第1讲 C语言第一阶段.doc│  │      │  └─视频│          第1讲 C语言第一阶段.mp4│          ├─2传智播客_尹成_C语言从菜鸟到高手_第二章C语言跨平台HelloWorld-A│  ├─第10节 2.5.1-2.5.7C

传智播客C语言视频第一季(有效下载期为10.1-10.7,10.8关闭)

?? J:\传智播客_尹成_C语言从菜鸟到高手├─传智播客_尹成_C语言从菜鸟到高手_第一章C语言概述A│      第一讲1.1C语言第一阶段.mp4│      第二讲1.2c语言入门教程.mp4│      ├─传智播客_尹成_C语言从菜鸟到高手_第七章编译选项_链表_栈_队列_C实战│  ├─7.1编译与预处理│  │      第10讲 7.1.14-24宏的高级用法2.mp4│  │      第11讲 7.1.25文件包含-7.1.28编译及预处理小节.mp4│  │      第