花了一天的时间,终于捣鼓出了一款别用特色的猜图小游戏. 那有什么特别之处了??
效果展示:
1>采用MVC设计模式
2>使用懒加载
- //懒加载
- - (NSArray *)guessArray{
- if (!_guessArray) {
- NSString *path = [[NSBundle mainBundle] pathForResource:@"questions.plist" ofType:nil];
- NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
-
- NSMutableArray *tempArray = [NSMutableArray array];
- for (NSDictionary *dict in dictArray) {
- YYGuessModel *guess = [YYGuessModel guessWithDict:dict];
- [tempArray addObject:guess];
- }
- _guessArray = tempArray;
- }
- return _guessArray;
- }
复制代码
3>字典转模型
- #import "YYGuessModel.h"
- @implementation YYGuessModel
- - (instancetype)initWithDict:(NSDictionary *)dict{
- if (self = [super init]) {
- [self setValuesForKeysWithDictionary:dict];
- }
- return self;
- }
- + (instancetype)guessWithDict:(NSDictionary *)dict{
- return [[self alloc] initWithDict:dict];
- }
- @end
复制代码
4>使用中间弹框,温馨提示用户(带自定义的枚举参数)
- //弹框(三种)
- -(void)showAlertView:(YYGuessAlertType)alertType{
- UIAlertAction *againAction = [UIAlertAction actionWithTitle:@"再来一次" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- self.index = -1;
- [self nextBtn:self.nextBtn];
- }];
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
- NSLog(@"你选择了取消");
- self.index --;
- [self nextBtn:self.nextBtn];
- }];
- UIAlertAction *continueAction = [UIAlertAction actionWithTitle:@"继续" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- NSLog(@"你选择了继续");
-
- if (self.index != self.guessArray.count - 1) {
- [self nextBtn:self.nextBtn];
- }
- }];
- UIAlertController *alert = nil;
- switch (alertType) {
- case YYGuessAlertError:
- {
- alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"sorry,您答错了..扣除100金币" preferredStyle:UIAlertControllerStyleAlert];
-
- [alert addAction:cancelAction];
- }
- break;
-
- case YYGuessAlertRight:
- {
- alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"恭喜您,您答对了! 奖励200金币" preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:continueAction];
- [alert addAction:cancelAction];
- }
- break;
-
- case YYGuessAlertPass:
- {
- alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"恭喜您,您已经通关!! 奖励1000金币" preferredStyle:UIAlertControllerStyleAlert];
- [alert addAction:againAction];
- [alert addAction:cancelAction];
- }
- break;
-
- }
-
- [self presentViewController:alert animated:YES completion:^{
- NSLog(@"....");
- }];
- }
复制代码
5>九宫格小算法
- //加载待选项按钮(只加载一次)
- -(void)loadOptionsBtn{
-
- CGFloat optionBtnW = 44;
- CGFloat optionBtnH = optionBtnW;
- CGFloat padding = 2;
- CGFloat xMargin = (SCREEN_W - 2 * padding - COL * optionBtnW) / (COL - 1);
-
- NSMutableArray *optBtnArray = [NSMutableArray array];
- for (int i = 0; i < COL * ROW; i++) {
- int col = i % COL;
- int row = i / COL;
-
- CGFloat optionBtnX = padding + (xMargin + optionBtnW) * col;
- CGFloat optionBtnY = padding + (padding + optionBtnH) * row;
-
- UIButton *optionBtn = [[UIButton alloc]initWithFrame:CGRectMake(optionBtnX, optionBtnY, optionBtnW, optionBtnH)];
- [optionBtn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
- [optionBtn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
-
- [optionBtn addTarget:self action:@selector(tipOptionBtn:) forControlEvents:UIControlEventTouchUpInside];
-
- optionBtn.tag = 101 + i;
-
- [self.optionsView addSubview:optionBtn];
- [optBtnArray addObject:optionBtn];
-
- }
- self.optBtnArray = optBtnArray;
-
- }
复制代码 6>动态创建答案按钮框
- - (void)loadAnswerBtn:(YYGuessModel *)guess{
- //移除上一次创建的按钮
- for (UIButton *btn in self.ansBtnArray) {
- [btn removeFromSuperview];
- }
-
- NSString *answer = guess.answer;
- CGFloat ansBtnW = 44;
- CGFloat ansBtnH = ansBtnW;
- CGFloat padding = 10;
-
- NSInteger length = answer.length;
- NSMutableArray *ansBtnArray = [NSMutableArray array];
- for (int i = 0; i < length; i++) {
- UIButton *ansBtn = [[UIButton alloc]init];
- [ansBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
- [ansBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
-
- CGFloat leftMargin = (SCREEN_W - ansBtnW * length - (length - 1)*padding) / 2;
- CGFloat ansBtnX = leftMargin + (ansBtnW + padding) * i;
- ansBtn.frame = CGRectMake(ansBtnX, 0, ansBtnW, ansBtnH);
-
- [ansBtn addTarget:self action:@selector(tipAnswerBtn:) forControlEvents:UIControlEventTouchUpInside];
-
- [self.answerView addSubview:ansBtn];
- [ansBtnArray addObject:ansBtn];
-
- }
- self.ansBtnArray = ansBtnArray;
-
- }
复制代码
|