超级猜图

2019-04-13 16:50发布

#import "ViewController.h" #import "CZquestions.h"
@interface ViewController ()<UIAlertViewDelegate>
//所有问题的数据都在这个数组中 @property(nonatomic,strong)NSArray * questions; @property (weak, nonatomic) IBOutlet UIButton *money;
//控制题目索引 @property(nonatomic,assign)int index;
//阴影 @property(nonatomic,weak)UIButton * cover; -(void)tapCover;
//保存原始大小 @property(nonatomic,assign)CGRect imgFrame;
@property (weak, nonatomic) IBOutlet UILabel *lblIndex;
@property (weak, nonatomic) IBOutlet UILabel *lbltitle;
@property (weak, nonatomic) IBOutlet UIButton *btnImg; - (IBAction)tapImg:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *btnNext; - (IBAction)nextQuestion;
- (IBAction)biggerImg;
@property (weak, nonatomic) IBOutlet UIView *answerView;
@property (weak, nonatomic) IBOutlet UIView *optionsView;
@property(nonatomic,assign)int tishiIndex; @property(nonatomic,strong)NSMutableString * tishiBtnAnswer; @property (weak, nonatomic) IBOutlet UIButton *btnTishi;
- (IBAction)tishi;


@end
@implementation ViewController
-(NSArray*)questions {     if (_questions == nil) {         NSString* path = [[NSBundle mainBundle]pathForResource:@"pic.plist" ofType:nil];         NSArray * array = [NSArray arrayWithContentsOfFile:path];         NSMutableArray * modelCZ = [NSMutableArray array];         for (NSDictionary * dict in array) {             CZquestions * cz = [CZquestions czquestionsWithDict:dict];             [modelCZ addObject:cz];                      }                  _questions = modelCZ;                       }     return _questions;      }
//改变状态栏的颜 {MOD}为白 {MOD} -(UIStatusBarStyle)preferredStatusBarStyle { //    return UIStatusBarStyleDefault;//状态栏的颜 {MOD}为深 {MOD}     return UIStatusBarStyleLightContent;//状态栏的颜 {MOD}为白 {MOD} }

//隐藏状态栏 -(BOOL)prefersStatusBarHidden {     return YES; }

- (void)viewDidLoad {     [super viewDidLoad];     // Do any additional setup after loading the view, typically from a nib.          self.tishiBtnAnswer = [NSMutableString string];          self.index = -1;          [self next]; //    self.cover = nil;
}
- (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];     // Dispose of any resources that can be recreated. }
- (IBAction)biggerImg {
    self.imgFrame = self.btnImg.frame;     CGFloat w = self.view.frame.size.width;     CGFloat h = w;     CGFloat x = 0;     CGFloat y = (self.view.frame.size.height - h) / 2;          CGRect viewFrame = self.view.frame;     self.cover = [UIButton buttonWithType:UIButtonTypeSystem];     self.cover.frame = viewFrame;     self.cover.backgroundColor = [UIColor whiteColor];     [self.view addSubview:self.cover];     self.cover.alpha = 0.0;     [UIView animateWithDuration:0.7 animations:^{         self.btnImg.frame = CGRectMake(x, y, w, h);         self.cover.alpha = 0.6;     }];          [self.cover addTarget:self action:@selector(tapCover) forControlEvents:UIControlEventTouchUpInside];          [self.view bringSubviewToFront:self.btnImg]; }
-(void)tapCover {
    [UIView animateWithDuration:0.7 animations:^{         self.btnImg.frame = self.imgFrame;         self.cover.alpha = 0.0;     } completion:^(BOOL finished) {         if (finished) {                          [self.cover removeFromSuperview];         }     }];          self.cover = nil;      }
- (IBAction)tapImg:(id)sender {
    if (self.cover == nil) {         [self biggerImg];     }else{         [self tapCover];     }      }
- (IBAction)nextQuestion {     [self next];      }
-(void)isTishiEnabled {     int score =self.money.currentTitle.intValue;     if (score - 1000 < 1000) {         self.btnTishi.enabled = NO;     }else{         self.btnTishi.enabled = YES;     }      }
-(void)next {     self.tishiIndex = 0;          [self isTishiEnabled];               if (self.index+1 == self.questions.count) {                  UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"操作提示" message:@"恭喜通关" delegate: nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];         alert.delegate = self;         [alert show];                  return;     } //    1. 索引++     self.index++;     CZquestions * model = self.questions[self.index];                //    2.切换显示     [self loadNextTopViewPart:model];
    [self loadNextBottomViewPart:model];          self.optionsView.userInteractionEnabled = YES;
     }
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {     if (buttonIndex == 1) {         self.index = -1;         [self next];     }

}
-(void)loadNextTopViewPart:(CZquestions*)model {     [self.lblIndex setText:[NSString stringWithFormat:@"%d / %lu",self.index + 1,self.questions.count]];     self.lbltitle.text = model.title;               [self.btnImg setImage:[UIImage imageNamed:model.icon] forState:UIControlStateNormal];     //    3.最后一题时,下一题禁用     self.btnNext.enabled = (self.index != self.questions.count - 1); }
-(void)loadNextBottomViewPart:(CZquestions*)model {     [self loadAnswerView:model];     [self loadOptionsView:model];      }
-(void)loadAnswerView:(CZquestions*)model {     [self.answerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];          NSUInteger length = [model.answer length];          NSLog(@"length = %lu",length);          CGFloat w = 35.0;     CGFloat h = 35.0;     CGFloat y = 0.0;     CGFloat margin = 10.0;     CGFloat leftMargin = (self.answerView.frame.size.width - length * w - (length - 1.0) * margin ) / 2.0;          for (int i = 0; i < length ; i++) {         UIButton * btnAnswer = [UIButton buttonWithType:UIButtonTypeCustom];         CGFloat x = leftMargin + (w + margin) * i;         btnAnswer.frame = CGRectMake(x, y, w, h);         [btnAnswer setBackgroundImage:[UIImage imageNamed:@"center_img"] forState:UIControlStateNormal];         [self.answerView addSubview:btnAnswer];         [btnAnswer setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];         [btnAnswer addTarget:self action:@selector(bthAnswerClick:) forControlEvents:UIControlEventTouchUpInside];     }
}
-(void)bthAnswerClick:(UIButton *)sender {     for (UIButton * btn in self.optionsView.subviews) {         if (btn.tag == sender.tag) {             btn.hidden = NO;             break;         }              }
    for (UIButton* btn in self.answerView.subviews) {         [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }     [sender setTitle:nil forState:UIControlStateNormal];     self.optionsView.userInteractionEnabled = YES; }
-(void)loadOptionsView:(CZquestions*)model {
    [self.optionsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];          int sum =(int)model.options.count;          CGFloat w = 35;     CGFloat h = 35;     CGFloat topY = 0;     CGFloat marginX = 10;     CGFloat marginY = marginX;     int Hsum = 7;     CGFloat leftMargin = (self.optionsView.frame.size.width - Hsum * w - (Hsum -1) * marginX ) / 2;          for (int i = 0; i < sum; i++) {         int hi = i % Hsum;         int vi = i / Hsum;                  UIButton * btnOptions =[UIButton buttonWithType:UIButtonTypeSystem];         CGFloat x = leftMargin + (w + marginX) * hi;         CGFloat y = topY + (h + marginY) * vi;         btnOptions.frame = CGRectMake(x, y, w, h);         [btnOptions setBackgroundImage:[UIImage imageNamed:@"center_img"] forState:UIControlStateNormal];         [self.optionsView addSubview:btnOptions];         [btnOptions setTitle:model.options[i] forState:UIControlStateNormal];         [btnOptions setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];                  [btnOptions addTarget:self action:@selector(bthOptionsClick:) forControlEvents:UIControlEventTouchUpInside];                  btnOptions.tag = i;          }
}
-(void)bthOptionsClick:(UIButton *)sender {     sender.hidden = YES;          NSString * strOptions = sender.currentTitle;          for (UIButton* btn in self.answerView.subviews) {         if (btn.currentTitle == nil) {             [btn setTitle:strOptions forState:UIControlStateNormal];
            btn.tag = sender.tag;             break;         }     }          BOOL isFool = YES;     NSMutableString * str = [NSMutableString string];     for (UIButton * btn in self.answerView.subviews) {         if (btn.currentTitle == nil) {             isFool = NO;             break;         }else{             [str appendString:btn.currentTitle];
        }     }          CZquestions * mod = self.questions[self.index];     if (isFool) {         if ([str isEqualToString:mod.answer]) {                         [self changAnswerTextColor:[UIColor blueColor]];              [self changeScore:1000];             [self performSelector:@selector(next) withObject:nil afterDelay:0.5];         }else{             [self changAnswerTextColor:[UIColor redColor]];         }                  }             self.optionsView.userInteractionEnabled = !isFool;

}
-(void)changeScore:(int)score {     int preScore = self.money.currentTitle.intValue;          [self.money setTitle:[NSString stringWithFormat:@"%d",preScore + score] forState:UIControlStateNormal];
}
-(void)changAnswerTextColor:(UIColor *)color {     for (UIButton * btn in self.answerView.subviews) {         [btn setTitleColor:color forState:UIControlStateNormal];     }
}

- (IBAction)tishi {     [self isTishiEnabled];     CZquestions * model = self.questions[self.index];          if ( self.tishiIndex > (int)model.answer.length - 1) {         return;     }          [self changeScore:-1000];     
    for (UIButton * btn in self.answerView.subviews) {         [self bthAnswerClick:btn];     }

              NSString * modelAnswer = [model.answer substringToIndex:self.tishiIndex + 1];          int i = 0;     for (UIButton*btnAns in self.answerView.subviews) {         if (i  == modelAnswer.length) {             break;         }                  NSString * word = [modelAnswer substringWithRange:NSMakeRange(i, 1)];                  for (UIButton * btnOpt in self.optionsView.subviews) {             if ([btnOpt.currentTitle isEqualToString:word]) {                 [self bthOptionsClick:btnOpt];                 break;             }         }                           i++;                           
    }          self.tishiIndex++;
    } @end
//plist 模型 #import
@interface CZquestions : NSObject
@property(nonatomic,strong)NSString * answer;
@property(nonatomic,strong)NSString * icon;
@property(nonatomic,strong)NSString * title;
@property(nonatomic,strong)NSArray * options;
-(instancetype)initWithDict:(NSDictionary*)dict;
+(instancetype)czquestionsWithDict:(NSDictionary*)dict;

@end


#import "CZquestions.h"
@implementation CZquestions
- (instancetype)initWithDict:(NSDictionary *)dict {     self = [super init];     if (self) {         self.answer = dict[@"answer"];         self.title = dict[@"title"];         self.icon = dict[@"icon"];         self.options = dict[@"options"];     }     return self; }
+(instancetype)czquestionsWithDict:(NSDictionary *)dict {     return [[self alloc]initWithDict:dict]; }

@end


热门文章