IOS UI开发基础之超级猜图完整版本-08
生活随笔
收集整理的這篇文章主要介紹了
IOS UI开发基础之超级猜图完整版本-08
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
IOS UI開(kāi)發(fā)基礎(chǔ)之超級(jí)猜圖完整版本-08
// // ViewController.m // 09-超級(jí)猜圖 // // 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(@"答題完畢");//彈出對(duì)話框 // UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"操作提示" message:@"恭喜通關(guān)" 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{//隱藏當(dāng)前被電擊的按鈕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.判斷答案按鈕是否已經(jīng)填滿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. 如果答案按鈕被填滿了,判斷用戶點(diǎn)擊的答案是否與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];}//統(tǒng)一設(shè)置答案按鈕的文字顏色 -(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];}}//加載數(shù)據(jù),把模型數(shù)據(jù)設(shè)置到界面的控件上 -(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);} //創(chuàng)建答案按鈕 -(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];}} //答案按鈕點(diǎn)擊事件 -(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-超級(jí)猜圖 // // 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-超級(jí)猜圖 // // 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總結(jié)
以上是生活随笔為你收集整理的IOS UI开发基础之超级猜图完整版本-08的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 软件质量管理之困境与对策思考
- 下一篇: 周鸿祎详解360手机战略:赚钱不靠硬件靠