您的位置:首页 > 移动开发 > IOS开发

iOS实现(超级猜图)源码

2016-03-09 22:34 525 查看
//首先建立模型文件
QLLQuestion.hheQLLQuestion.m文件

#import <Foundation/Foundation.h>

@interface QLLQuestion : NSObject
@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;
@end

#import "QLLQuestion.h"

@implementation QLLQuestion
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
self.answer=dict[@"answer"];
self.icon=dict[@"icon"];
self.title=dict[@"title"];
self.options=dict[@"options"];
}
return self;
}

+(instancetype)QuestionWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];

}
@end

//
//  ViewController.m
//  01-超级猜图
//
//

#import "ViewController.h"
#import "QLLQuestion.h"
@interface ViewController ()
- (IBAction)tip;
- (IBAction)help;
- (IBAction)bigImg;
- (IBAction)nextQuestion;
- (IBAction)iconClick;
/**
*  待选项视图
*/
@property (weak, nonatomic) IBOutlet UIView *opintionView;
/**
*  答案项视图
*/
@property (weak, nonatomic) IBOutlet UIView *answerView;
/**
*  模型类
*/
@property(nonatomic,strong)NSArray *question;
/**
*  初始化序号
*/
@property(nonatomic,assign)int index;
/**
*  序号label
*/
@property (weak, nonatomic) IBOutlet UILabel *numLabel;
/**
*  标题
*/
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
/**
*  图片按钮
*/
@property (weak, nonatomic) IBOutlet UIButton *imageBtn;
/**
*  下一题按钮
*/
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
/**
*  阴影按钮
*/
@property (weak, nonatomic) IBOutlet UIButton *cover;
/**
*  分数按钮
*/
@property (weak, nonatomic) IBOutlet UIButton *scoreBtn;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//初始化index
self.index=-1;
[self nextQuestion];
}
/**
*  延时加载
*/
-(NSArray *)question{
if (_question==nil) {

//引入plist文件
NSArray *dicArray=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"questions" ofType:@"plist"]];
NSMutableArray *questonsAray=[NSMutableArray array];
//取出模型
for (NSDictionary *dict in dicArray) {
QLLQuestion *quenstions=[[QLLQuestion alloc]initWithDict:dict];
[questonsAray addObject:quenstions];
}
//赋值
_question=questonsAray;
}
return  _question;
}
-(UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;

}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
*  下一题
*/
- (IBAction)nextQuestion {
//增加索引
self.index++;

//取出模型
QLLQuestion *quenstion=self.question[self.index];
//    NSLog(@"%d",self.index);
//设置数据
[self settingDateWithQuenstion:quenstion];
//设置答案项
[self addAnswerBtn:quenstion];
//设置待选项
[self addOpinionBtn:quenstion];
}
/**
*  设置数据
*/
-(void)settingDateWithQuenstion:(QLLQuestion *)quenstion{
self.numLabel.text=[NSString stringWithFormat:@"%d/%lu",self.index+1,self.question.count];
self.titleLabel.text=quenstion.title;
[self.imageBtn setImage:[UIImage imageNamed:quenstion.icon] forState:UIControlStateNormal];
//设置下一个按钮的状态
self.nextBtn.enabled=(self.index!=self.question.count-1);
}
/**
*  设置答案项
*/
-(void)addAnswerBtn:(QLLQuestion *)quenstion{

//删除之前的所有按钮
//    for (UIView *subView in self.answerView.subviews) {
//        [subView removeFromSuperview];
//    }
[self.answerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//设置答案按钮
NSInteger length=quenstion.answer.length;
for (int i=0; i<length; i++) {
UIButton *answerBtn=[[UIButton alloc]init];
[self.answerView addSubview:answerBtn];
//设置背景
[answerBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
[answerBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
[answerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//设置frame
CGFloat margin=10;
CGFloat answerW=40;
CGFloat answerH=40;
CGFloat viewW=self.view.frame.size.width;
CGFloat leftMargin=(viewW-length*answerW-(length-1)*margin)*0.5;
CGFloat answerX=leftMargin+i*(answerW+margin);
answerBtn.frame=CGRectMake(answerX, 0, answerW, answerH);
//监听答案按钮的点击
[answerBtn addTarget:self action:@selector(answerClick:) forControlEvents:UIControlEventTouchUpInside];
[self.answerView addSubview:answerBtn];
}

}
/**
*  监听答案按钮的点击
*/
-(void)answerClick:(UIButton *)answerBtn{
//答案按钮的文字
//    NSString *answerTitle=[answerBtn titleForState:UIControlStateNormal];
NSString *answerTitle=answerBtn.currentTitle;

//让答案按钮对应的待选项的文字显示出来
for (UIButton *optionBtn in self.opintionView.subviews) {
//待选项的文字
NSString *optionTitle=[optionBtn titleForState:UIControlStateNormal];

if ([optionTitle isEqualToString:answerTitle] && optionBtn.hidden==YES) {
optionBtn.hidden=NO;
break;
}
}
//    NSLog(@"%d",self.index);

//让被点击的答案按钮文字消失
[answerBtn setTitle:nil forState:UIControlStateNormal];
//让所有的答案按钮变为黑色
for (UIButton *answerBtn in self.answerView.subviews) {
[answerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
}
/**
*  设置带选项
*/
-(void)addOpinionBtn:(QLLQuestion *)quenstion{
//设置待选项
//删除之前的按钮
//让数组中所有的对象都调用removeFromSuperview
[self.opintionView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//    for (UIView *subview in self.opintionView.subviews) {
//        [subview removeFromSuperview];
//    }

NSInteger count=quenstion.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];
//设置文字
[optionBtn setTitle:quenstion.options[i] forState:UIControlStateNormal];
[optionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

optionBtn.titleLabel.font=[UIFont systemFontOfSize:23];

NSInteger totalColumns=7;
//设置frame
CGFloat margin=13;
NSInteger row=i/totalColumns;
NSInteger col=i%totalColumns;
CGFloat optionW=45;
CGFloat optionH=45;
CGFloat viewW=self.view.frame.size.width;
CGFloat optionLeftMargin=(viewW-totalColumns*optionW-(totalColumns-1)*margin)*0.5;

CGFloat optionX=optionLeftMargin + col*(optionW+margin);
CGFloat optionY=row*(optionH+margin);
optionBtn.frame=CGRectMake(optionX,optionY , optionW, optionH);
//监听待选按钮的点击
[optionBtn addTarget:self action:@selector(optionClick:) forControlEvents:UIControlEventTouchUpInside];

[self.opintionView addSubview:optionBtn];

}

}
/**
*  监听待选按钮的点击
*/
-(void)optionClick:(UIButton *)optionBtn{
//让待选按钮消失
optionBtn.hidden=YES;

//显示文字到正确答案上
for (UIButton *answerBtn in self.answerView.subviews) {
//判断按钮是否有文字
NSString *ansTitle= [answerBtn titleForState:UIControlStateNormal];
if(ansTitle.length==0){ //没有文字
//设置答案按钮的文字为被点击待选按钮的文字
NSString *optionTitle= [optionBtn titleForState:UIControlStateNormal];

[answerBtn setTitle:optionTitle forState:UIControlStateNormal];

break;
}
}
//判断答案是否填满
BOOL full=YES;
NSMutableString *tempAnswerTitle=[NSMutableString string];
for (UIButton *answerBtn in self.answerView.subviews) {
//判断按钮是否有文字
NSString *ansTitle= [answerBtn titleForState:UIControlStateNormal];
if(ansTitle.length==0){ //没有文字

full=NO;
}
//拼接文字
if (ansTitle) {
[tempAnswerTitle appendString:ansTitle];
}
}

//答案满 了
if (full) {
QLLQuestion *quenstion=self.question[self.index];
if ([tempAnswerTitle isEqualToString:quenstion.answer]) {   //答对了,文字蓝色
for (UIButton *answerBtn in self.answerView.subviews) {
[answerBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}
//加分
[self scoreDelta:500];

//跳到下一题
[self performSelector:@selector(nextQuestion) withObject:nil afterDelay:0.25];

}else{
for (UIButton *answerBtn in self.answerView.subviews) {
[answerBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
}
}
/**
*  点击图片变大和变小
*/
- (IBAction)iconClick {

if (self.cover==nil) {//要放大
[self bigImg];
}else{
[self smallImg];
}
}
/**
*  提示
*/
- (IBAction)tip {
//点击所有的答案按钮
for (UIButton *answerBtn in self.answerView.subviews) {
[self answerClick:answerBtn];
}

//取出答案
QLLQuestion *quenstions=self.question[self.index];

//
NSString *firstAnswer= [quenstions.answer substringToIndex:1];

for (UIButton *optionBtn in self.opintionView.subviews) {
if ([optionBtn.currentTitle isEqualToString:firstAnswer]) {
[self optionClick:optionBtn];
break;
}
}
//  扣分
[self scoreDelta:-700];
}
/**
*  帮助
*/
- (IBAction)help {
}
-(void)scoreDelta:(int)delta{
int score=self.scoreBtn.currentTitle.intValue;
score+=delta;
[self.scoreBtn setTitle:[NSString stringWithFormat:@"%d",score] forState:UIControlStateNormal];
}
/**
*  大图
*/
- (IBAction)bigImg {

//添加阴影
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;

//开始动画
//更换阴影和头像的位置
//
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:1.0];
[UIView animateWithDuration:0.5 animations:^{
[self.view bringSubviewToFront:self.imageBtn];
CGFloat iconW=self.view.frame.size.width;
CGFloat iconH=iconW;
CGFloat iconX=0;
CGFloat iconY=(self.view.frame.size.height-iconH)*0.5;
self.imageBtn.frame=CGRectMake(iconX, iconY, iconW, iconH);
cover.alpha=0.7;
}];

//    [UIView commitAnimations];
}
/**
*  小图
*/
-(void)smallImg{
//动画的block

[UIView animateWithDuration:0.5 animations:^{
self.imageBtn.frame=CGRectMake(97, 160, 220, 220);
self.cover.alpha=0.0;
} completion:^(BOOL finished) {
[self.cover removeFromSuperview];
self.cover=nil;
}];

//删除阴影
//    [self.cover removeFromSuperview];
//    self.cover=nil;;
//缩小图片
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:1.0];
//    [UIView setAnimationDelegate:self];
//    [UIView setAnimationDidStopSelector:@selector(removeCover)];
//    self.imageBtn.frame=CGRectMake(97, 160, 220, 220);
//
//
//    self.cover.alpha=0.0;

//    [UIView commitAnimations];

}
//
//-(void)removeCover{
//    [self.cover removeFromSuperview];
//    self.cover=nil;
//}
@end


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: