IOS UI开发基础之超级猜图完整版本-08

//
//  ViewController.m
//  09-超级猜图
//
//  Created by 鲁军 on 2021/1/31.
//#import "ViewController.h"
#import "CZQuestion.h"@interface ViewController ()
- (IBAction)btnIconClick:(id)sender;@property(nonatomic,assign)CGRect iconFrame;@property(nonatomic,strong)NSArray *questions;@property(nonatomic,assign)int index;@property (weak, nonatomic) IBOutlet UILabel *lblIndex;@property (weak, nonatomic) IBOutlet UIButton *btnScore;
@property (weak, nonatomic) IBOutlet UILabel *lblTitle;
@property (weak, nonatomic) IBOutlet UIButton *btnIcon;
@property (weak, nonatomic) IBOutlet UIButton *btnNext;
@property (weak, nonatomic) UIButton *cover;@property (weak, nonatomic) IBOutlet UIView *answerView;
@property (weak, nonatomic) IBOutlet UIView *optionsView;- (IBAction)btnTipClick;- (IBAction)btnNextClick;- (IBAction)bigImage:(id)sender;@end@implementation ViewController- (NSArray *)questions{if(_questions==nil){NSString *path = [[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil];NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];NSMutableArray *arrMutable = [NSMutableArray array];for (NSDictionary *dict in arrayDict) {CZQuestion *model = [CZQuestion questionWithDict:dict];[arrMutable addObject:model];}_questions = arrMutable;}return _questions;
}- (UIStatusBarStyle)preferredStatusBarStyle{return UIStatusBarStyleLightContent;
}- (BOOL)prefersStatusBarHidden{return YES;
}- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.self.index = -1;[self nextQuestion];}- (IBAction)bigImage:(id)sender {self.iconFrame = self.btnIcon.frame;UIButton *btnCover = [[UIButton alloc] init];btnCover.frame = self.view.bounds;btnCover.backgroundColor = [UIColor blackColor];btnCover.alpha = 0.0;[self.view addSubview:btnCover];[btnCover addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];[self.view bringSubviewToFront:self.btnIcon];self.cover = btnCover;CGFloat iconW = self.view.frame.size.width;CGFloat iconH = iconW;CGFloat iconX = 0;CGFloat iconY =( self.view.frame.size.height - iconH)*0.5;[UIView animateWithDuration:0.7 animations:^{btnCover.alpha = 0.6;self.btnIcon.frame = CGRectMake(iconX, iconY, iconW, iconH);}];}- (IBAction)btnNextClick {// NSLog(@"aaa");[self nextQuestion];}- (IBAction)btnTipClick {[self addScore:-1000];for(UIButton *btnAnswer in self.answerView.subviews){[self btnAnswerClick:btnAnswer];}CZQuestion *model = self.questions[self.index];NSString *firstChar = [model.answer substringToIndex:1];for(UIButton *btnOpt in self.optionsView.subviews){if([btnOpt.currentTitle isEqualToString:firstChar]){[self optionButtonClick:btnOpt];break;}}
}-(void)nextQuestion{self.index++;if(self.index == self.questions.count){NSLog(@"答题完毕");//弹出对话框
//        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"操作提示" message:@"恭喜通关" delegate:nil cancelButtonTitle:@"*取消*" otherButtonTitles:@"确定",@"哈哈哈"  , nil];
//
//        [alertView show];return;}CZQuestion *model=self.questions[self.index];[self settingData:model];[self makeAnswerButton:model];[self makeOptionsButton:model];}-(void)makeOptionsButton:(CZQuestion *)model{self.optionsView.userInteractionEnabled = YES;[self.optionsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];NSArray *words =model.options;CGFloat optionW = 35,optionH=35;CGFloat margin =10;int colums  = 7;CGFloat marginLeft = (self.optionsView.frame.size.width - colums*optionW - (colums-1)*margin)*0.5;for(int i=0;i<words.count;i++){UIButton *btnOpt = [[UIButton alloc] init];btnOpt.tag = i;[btnOpt setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];[btnOpt setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];[btnOpt setTitle:words[i] forState:UIControlStateNormal];[btnOpt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];int colIdx = i%colums;int rowIdx = i/colums;CGFloat optionX = marginLeft + colIdx*(optionW+margin);CGFloat optionY = 0+ rowIdx*(optionH+margin);btnOpt.frame = CGRectMake(optionX, optionY, optionW, optionH);[self.optionsView addSubview:btnOpt];[btnOpt addTarget:self action:@selector(optionButtonClick:) forControlEvents:UIControlEventTouchUpInside];}}
//待选按钮的单击事件
-(void)optionButtonClick:(UIButton *)sender{//隐藏当前被电击的按钮sender.hidden = YES;
//    NSString *text = sender titleForState:UIControlStateNormal;NSString *text = sender.currentTitle;for(UIButton *answerBtn in self.answerView.subviews){if(answerBtn.currentTitle==nil){[answerBtn setTitle:text forState:UIControlStateNormal];answerBtn.tag = sender.tag;break;}}//3.判断答案按钮是否已经填满BOOL isFull = YES;NSMutableString *userInput = [NSMutableString string];for(UIButton *btnAnswer in self.answerView.subviews){if(btnAnswer.currentTitle==nil){isFull = NO;break;}else{[userInput appendString:btnAnswer.currentTitle];}}if(isFull){self.optionsView.userInteractionEnabled = NO;//4. 如果答案按钮被填满了,判断用户点击的答案是否与CZQuestion *model=self.questions[self.index];if([model.answer isEqualToString:userInput]){[self addScore:100];[self setAnswerButtonColor:[UIColor blueColor]];[self performSelector:@selector(nextQuestion) withObject:nil afterDelay:0.5];}else{[self setAnswerButtonColor:[UIColor redColor]];}}
}-(void)addScore:(int)score{NSString *str= self.btnScore.currentTitle;int currentScore = str.intValue;currentScore = currentScore +score;[self.btnScore setTitle:[NSString stringWithFormat:@"%d",currentScore] forState:UIControlStateNormal];}//统一设置答案按钮的文字颜色
-(void)setAnswerButtonColor:(UIColor *)color{for(UIButton *btnAnswer in self.answerView.subviews){[btnAnswer setTitleColor:color forState:UIControlStateNormal];}}-(void)smallImage{[UIView animateWithDuration:0.7 animations:^{self.btnIcon.frame = self.iconFrame;self.cover.alpha = 0.0;} completion:^(BOOL finished) {[self.cover removeFromSuperview];self.cover = nil;}];}
- (IBAction)btnIconClick:(id)sender {if(self.cover==nil){[self bigImage:nil];}else{[self smallImage];}}//加载数据,把模型数据设置到界面的控件上
-(void)settingData:(CZQuestion *)model{self.lblIndex.text=[NSString stringWithFormat:@"%d / %ld",(self.index + 1),self.questions.count];self.lblTitle.text=model.title;[self.btnIcon setImage:[UIImage imageNamed:model.icon] forState:UIControlStateNormal];self.btnNext.enabled = (self.index != self.questions.count - 1);}
//创建答案按钮
-(void)makeAnswerButton:(CZQuestion *)model{//    while(self.answerView.subviews.firstObject){//        [self.answerView.subviews.firstObject removeFromSuperview];
//    }[self.answerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];NSUInteger len = model.answer.length;CGFloat margin = 10;CGFloat answerW = 35;CGFloat answerH = answerW;CGFloat answerY = 0;CGFloat marginLeft = (self.answerView.frame.size.width-(len*answerW)-(len-1)*margin)/2;for(int i=0;i<len;i++){UIButton *btnAnswer =[[UIButton alloc] init];[btnAnswer setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];[btnAnswer setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];CGFloat answerX = marginLeft + i * (answerW + margin);btnAnswer.frame = CGRectMake(answerX, answerY, answerW, answerH);[btnAnswer setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];[self.answerView addSubview:btnAnswer];[btnAnswer addTarget:self action:@selector(btnAnswerClick:) forControlEvents:UIControlEventTouchUpInside];}}
//答案按钮点击事件
-(void)btnAnswerClick:(UIButton *) sender {self.optionsView.userInteractionEnabled = YES;[self setAnswerButtonColor:[UIColor blackColor]];for(UIButton *optBn in self.optionsView.subviews){//        if([sender.currentTitle isEqualToString:optBn.currentTitle]){//            optBn.hidden = NO;
//            break;
//        }if(sender.tag==optBn.tag){optBn.hidden = NO;break;}}[sender setTitle:nil forState:UIControlStateNormal];
}
@end
//
//  CZQuestion.h
//  09-超级猜图
//
//  Created by 鲁军 on 2021/1/31.
//#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface CZQuestion : 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;@endNS_ASSUME_NONNULL_END
//
//  CZQuestion.m
//  09-超级猜图
//
//  Created by 鲁军 on 2021/1/31.
//#import "CZQuestion.h"@implementation CZQuestion- (instancetype)initWithDict:(NSDictionary *)dict{if(self==[super init]){self.answer = dict[@"answer"];self.title=dict[@"title"];self.icon=dict[@"icon"];self.options=dict[@"options"];}return  self;
}
+ (instancetype)questionWithDict:(NSDictionary *)dict{return [[self alloc] initWithDict:dict];
}@end

IOS UI开发基础之超级猜图完整版本-08相关推荐

  1. iOS UI 开发按钮的使用

    IOS UI 开发之按钮的使用 // // ViewController.m // 02按钮的使用介绍 // // Created by 鲁军 on 2021/1/26. //#import &quo ...

  2. ios直播开发基础,推流协议及流程

    一:推流需要的三方库和一些常用格式和协议介绍 1.rtmp协议 :实时消息传输协议,Adobe Systems公司为Flash播放器和服务器之间音频.视频和数据传输开发的开 放协议,因为是开放协议所以 ...

  3. Objective-C ,ios,iphone开发基础:UIAlertView使用详解

    UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...

  4. [置顶] Objective-C ,ios,iphone开发基础:命名规范

    命名规范:http://bukkake.iteye.com/blog/695492  点击打开链接 转载于:https://www.cnblogs.com/pangblog/p/3292256.htm ...

  5. Objective-C ,ios,iphone开发基础:NSDictionary(字典) 和 NSMutableDictionary

    NSDictionary(字典),NSDictionary类似于 .net中的parameter,l类似于java中的map. 通过唯一的key找到对应的值,一个key只能对应一个只,而多个key可以 ...

  6. [置顶] Objective-C,/,ios,/iphone开发基础:分类(category,又称类别)

    在c++中我们可以多继承来实现代码复用和封装使程序更加简练.在objective-c中只能单继承,不能多继承,那么除了协议protocol之外,我们可以实现类似多继承的一个方法就是,分类(catego ...

  7. iOS 应用开发基础翻译 改为 笔记和总结

    实在是翻译能力不高,翻译出来的中国话怎么读都别扭,所以此系列改为阅读笔记和总结,FYI. 转载于:https://www.cnblogs.com/csusheep/p/4459623.html

  8. Objective-C ,ios,iphone开发基础:picker控件详解与使用,(实现省市的二级联动)

    第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试,现在用不着. 点击next  ->creat之 ...

  9. Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作...

    SQLite  是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...

最新文章

  1. 在博客园添加Lisp(或其它)代码高亮
  2. 如何用python画数据图-关于如何使用Python绘制基本数据图形模型
  3. poj 3304 Segments (几何 : 线段 直线相交)
  4. 建信金融科技春季全国笔试卷B编程题
  5. python中def root的用法_python scipy optimize.root_scalar用法及代码示例
  6. Java回文数.如12321,123454321(5个数)
  7. Linux内核分析 - 网络[三]:从netif_receive_skb()说起
  8. python学习_Python学习资料整理
  9. 2×3卡方检验prism_戏说卡方检验
  10. 鸿蒙系统hdc,HDC2020有看头:要揭开鸿蒙系统和EMUI11神秘面纱?
  11. Linux 下安装 Wordpress教程
  12. 42条微信营销小技巧!
  13. AlexNet网络介绍
  14. Android源码编译遇到Java虚拟机内存不够等相关Jack问题,解决方法
  15. Elasticsearch的基本使用
  16. 【原创纯手打】如何用VUE在拖拽小框中同步更换壁纸(附代码)
  17. Solidworks安装
  18. python中reset_在Python中重置类的首选方法
  19. 想剑网三妹子最多服务器,《剑网3》新版本剧透 人气主角陈月妹子大显身手
  20. 腾讯云安装docker

热门文章

  1. nsa服务器win7系统,Win7系统访问NAS和Samba服务器失败怎么处理
  2. python 批量下载图片_Python 批量下载图片示例
  3. php和mysql web开发 笔记_PHP和MySQL Web开发读书笔记---创建Web数据库
  4. ubuntu导入第三方库_在Ubuntu中,如何添加Apt存储库
  5. jquery导入数据_Web技术——简单的数据库编程
  6. java在进行修改时报400_java开发注册群组报错400
  7. 电路中的这些符号标识,你真的明白吗?
  8. 使用VHDL编程的直接扩频发生器
  9. php毕设周记_毕设周记
  10. java class 生成对象_面向对象编程,你知道Java有哪些创建对象的方式吗?