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

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;


@end

NS_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值