需求分析
1.搭建界面
1>上半部分,固定的,用Storyboard直接连线
2>下半部分,根据题目的变化,不断变化和调整,用代码的方式实现会比较合适
*备选按钮区域
*答案按钮区域
2.编写代码
1>大图,小图的切换
2>下一题
3>备选按钮的点击,让文字进入答案区
4>判断胜负
*胜利:进入下一题
*失败:提示用户重新选择
5>答题按钮的点击
把答案区的文字回复到备选区域
3.收尾工作:图标和启动图片
课程重点:MC
今天课程以游戏代码的逻辑实现为主,不会过分抽取代码
小知识点:Highlighted Adjusts Image; 按钮没有点击动画
User Interation Enabled;不和用户交互
//
// HMViewController.m
// 01-超级猜图
//
// Created by apple on 14-8-15.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "HMViewController.h"
@interface HMViewController ()
@property (weak, nonatomic) IBOutlet UIButton *iconButton;
@property (nonatomic, strong) UIButton *cover;
@end
@implementation HMViewController
- (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(smallImage:) forControlEvents:UIControlEventTouchUpInside];
}
return _cover;
}
/**ios7调整状态栏颜色
返回的是一个枚举
UIStatusBarStyleDefault = 0, 黑色状态栏
UIStatusBarStyleLightContent NS_ENUM_AVAILABLE_IOS(7_0) = 1, 亮色状态栏
*/
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
/**
* 大图
*/
- (IBAction)bigImage
{
// 1. 添加蒙板(遮罩)
// UIButton *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(smallImage:) forControlEvents:UIControlEventTouchUpInside];
[self cover];//调用自己的工作方法
// 2. 将图像按钮弄到最前面
// bringSubviewToFront将子视图前置
[self.view bringSubviewToFront:self.iconButton];
// 3. 动画放大图像按钮
CGFloat w = self.view.bounds.size.width;
CGFloat h = w;
CGFloat y = (self.view.bounds.size.height - h) * 0.5;
[UIView animateWithDuration:1.0f animations:^{
self.iconButton.frame = CGRectMake(0, y, w, h);
self.cover.alpha = 1.0;
}];
//记得不选择自动布局,才会有放大动画
}
/**
* 小图
*/
- (void)smallImage:(UIButton *)cover
{
// 动画一但定义,马上开始
[UIView animateWithDuration:1.0 animations:^{
// 将图像恢复初始位置
self.iconButton.frame = CGRectMake(85, 85, 150, 150);
cover.alpha = 0.0;
} completion:^(BOOL finished) {
// 动画完成之后删除cover
// [cover removeFromSuperview];
}];
}
@end
字典转模型
.h
//
// QuestionList.h
// 0815-超级猜图
//
// Created by susan on 16/1/20.
// Copyright © 2016年 susan. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface QuestionList : UIView
@property (nonatomic,copy) NSString *answer;
@property (nonatomic,copy) NSString *icon;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,strong) NSArray *options;
/*使用字典实例化模型*/
-(instancetype) initWithDict:(NSDictionary *)dict;
/*类方法可以快速实例化一个对象*/
+(instancetype) questionWithDict:(NSDictionary *) dict;
/*返回所有题目数组*/
+(NSArray *)questions;
@end
.m
//
// QuestionList.m
// 0815-超级猜图
//
// Created by susan on 16/1/20.
// Copyright © 2016年 susan. All rights reserved.
//
#import "QuestionList.h"
@implementation QuestionList
//在成员方法中,如果给self赋值,只能initXX方法中进行
/*语法约定
1>所有的方法首字母小写
2>当单词切换的时候,单词首字母大写(驼峰法)
3>类名要大写
*/
-(instancetype) initWithDict:(NSDictionary *)dict{
self=[super init];
if(self){
[self setValuesForKeysWithDictionary:dict];}
return self;
}
+(instancetype) questionWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
+(NSArray *)questions{
NSArray *array=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil]];
NSMutableArray *arrayM=[NSMutableArray array];
for(NSDictionary *dict in array){
[arrayM addObject:[self questionWithDict:dict]];
}
return arrayM;
}
//对象描述方法,类似于java的toString(),便于跟踪调试的
//建议:如果是自定义的模型,最好编写description方法,可以方便调试
-(NSString *)description{
return [NSString stringWithFormat:@"<%@: %p>{answer: %@,icon %@,title:%@,options:%@}",self.class,self,self.answer,self.icon,self.title,self.options];
//"<QuestionList: 0x7af2b8b0>",
//"<QuestionList: 0x7ab0d570>{answer: \U8d8a\U5149\U5b9d\U76d2,icon movie_ygbh,title:\U6076\U641e\U98ce\U683c\U7684\U559c\U5267\U5927\U7247,options:(\n \"\\U7ebf\",\n \"\\U8bdd\",\n \"\\U98de\",\n \"\\U4eba\",\n \"\\U7b11\",\n \"\\U8d8a\",\n \"\\U9ec4\",\n \"\\U5de5\",\n \"\\U5927\",\n \"\\U8d85\",\n \"\\U4f20\",\n \"\\U7279\",\n \"\\U5b9d\",\n \"\\U9e3f\",\n \"\\U961f\",\n \"\\U8d64\",\n \"\\U5149\",\n \"\\U4eba\",\n \"\\U58c1\",\n \"\\U76d2\",\n \"\\U738b\"\n)}",
/* <QuestionList: 0x7978ede0>{answer: 王心凌,icon people_wangxinling,title:台湾演员、歌手,options:(
少,
王,
凌,
依,
泽,
亚,
岛,
林,
依,
松,
心,
枫,
利,
小,
蔡,
曹,
颖,
林,
玛,
晨,
芬,
)
},
*/
}
@end
字典显示中文+descrption
NSArray+Log.h
NSArray+Log.m
// 块代码 NSArray *array = @[@(1), @(2), @(3), @(4), @(5)];
循环遍历
- (void)arrayWith:(NSArray *)array
{
int i = 0;
for (NSNumber *num in array) {
NSLog(@"%@", num);
if (i == 1) {
break;
}
i++;
}
// 参数:对象,索引,是否中断
// 数组的块方法遍历的效率比for in高
[array enumerateObjectsUsingBlock:^(NSNumber *obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@", obj);
// idx == 1 退出循环
if (idx == 1) {
*stop = YES;
}
}];
}
排序
- (void)sortWith:(NSArray *)array
{
// 排序
array = [array sortedArrayUsingComparator:^NSComparisonResult(NSNumber *num1, NSNumber *num2) {
/**
1 4 5 2
4 1 5 2
4 1 5 2
5 4 1 2
5 4 1 2
5 4 2 1
*/
NSLog(@"%@ %@", num1, num2);
// 升序
// return [num1 compare:num2];
// 降序
return [num2 compare:num1];
}];
NSLog(@"%@", array);
}
乱序
// 排序
array = [array sortedArrayUsingComparator:^NSComparisonResult(NSNumber *num1, NSNumber *num2) {
// 乱序=>一会升序,一会降序
// 随机
// arc4random_uniform(10) => 0~9之间的随机数
int seed = arc4random_uniform(2);
if (seed) {
return [num1 compare:num2];
} else {
return [num2 compare:num1];
}
}];
NSLog(@"%@", array);