首页 > 代码库 > 超级猜图,小case,运用知识点: storyboard block封装变化点 蒙板 模态model
超级猜图,小case,运用知识点: storyboard block封装变化点 蒙板 模态model
最近这几天,不知道突然玩超级猜图玩疯了,最后觉得还是不过瘾,干脆下了ipa,运用素材自己写了一个,没想到居然实现了大部分功能,真开心,直接上图:
上代码,不足之处,还望牛人支出,不胜感激:
1 #import "JHViewController.h" 2 #import "JHAppDate.h" 3 4 @interface JHViewController () 5 { 6 NSArray *_arrayImage; // 内容数组 7 int _index; // 图片索引 8 UIView *_push; // 遮盖; 9 } 10 /** 翻页 */ 11 @property (weak, nonatomic) IBOutlet UILabel *pageTitle; 12 /** 钱数 */ 13 @property (weak, nonatomic) IBOutlet UILabel *coinTotal; 14 /** 标题 */ 15 @property (weak, nonatomic) IBOutlet UILabel *titleImage; 16 /** 图片 */ 17 @property (weak, nonatomic) IBOutlet UIButton *image; 18 /** 下一题按钮 */ 19 @property (weak, nonatomic) IBOutlet UIButton *nextImages; 20 /** 文字库 */ 21 @property (weak, nonatomic) IBOutlet UIView *wordView; 22 /** 选中文字 */ 23 @property (weak, nonatomic) IBOutlet UIView *selectWord; 24 25 /** 图片提示 */ 26 - (IBAction)tipImage; 27 /** 图片帮助 */ 28 - (IBAction)helpImage; 29 /** 大图显示 */ 30 - (IBAction)bigImage; 31 /** 下一题 */ 32 - (IBAction)nextImage; 33 /** 点击图片放大 */ 34 - (IBAction)clickImage; 35 36 @end 37 38 @implementation JHViewController 39 40 - (void)viewDidLoad 41 { 42 /** 加载数据 */ 43 _arrayImage = [JHAppDate appQuestions]; 44 45 _index = -1; 46 [self nextImage]; 47 [super viewDidLoad]; 48 49 } 50 52 /** 帮助 */ 53 - (IBAction)helpImage { 54 XHLog(@"功能实现中"); 55 } 56 57 /** 大图显示 */ 58 - (IBAction)bigImage { 59 [self changeImage]; 60 } 61 /** 点击图片放大 */ 62 - (IBAction)clickImage { 63 [self changeImage]; 64 } 65 66 /** 下一题 */ 67 - (IBAction)nextImage { 68 69 _index++; 70 _nextImages.enabled = _index != _arrayImage.count -1; 71 JHAppDate *appDate = _arrayImage[_index]; 72 // 基本数据设置 73 [self setBaseMassage:appDate]; 74 // 设置正确答案 75 [self setCorrectAnswer:appDate]; 76 // 设置备选答案 77 [self setCompareAnswer:appDate]; 78 79 } 80 81 /** 基本数据设置 */ 82 - (void) setBaseMassage: (JHAppDate *)appDate{ 83 NSString *str = [NSString stringWithFormat:@"%d / %d", _index+1, _arrayImage.count]; 84 _pageTitle.text = str; 85 _titleImage.text = appDate.title; 86 [_image setImage:[UIImage imageNamed:appDate.icon] forState:UIControlStateNormal]; 87 } 88 89 /** 90 * 设置正确答案 91 */ 92 -(void) setCorrectAnswer: (JHAppDate *)appDate{ 93 /** 清空_selectWord中按钮 */ 94 [_selectWord.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 95 CGFloat selfW = self.view.frame.size.width; 96 CGFloat marginX =10; 97 CGFloat wordBtnW = 35; 98 CGFloat wordBtnH = 35; 99 int count = appDate.answer.length; 100 CGFloat wordBtnMargin = (selfW - count *(wordBtnW + marginX)) * 0.5; 101 for (int i = 0; i< count; i++) { 102 UIButton *wordBtn = [[UIButton alloc] init]; 103 wordBtn.frame = CGRectMake(wordBtnMargin + (i *(wordBtnW + marginX)), 0, wordBtnW, wordBtnH); 104 [wordBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal]; 105 [wordBtn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted]; 106 [wordBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 107 [wordBtn addTarget:self action:@selector(answerBtn:) forControlEvents:UIControlEventTouchUpInside]; 108 [_selectWord addSubview:wordBtn]; 109 } 110 111 } 112 /** 113 * 设置正确答案点击 114 * 115 * @param wordBtn 被点击按钮 116 */ 117 - (void) answerBtn: (UIButton *)wordBtn{ 118 NSString *str = [wordBtn titleForState:UIControlStateNormal]; 119 120 for (UIButton *selectBtn in _wordView.subviews) { 121 NSString *str1 = [selectBtn titleForState:UIControlStateNormal]; 122 if ([str isEqualToString:str1]) { 123 selectBtn.hidden = NO; 124 break; 125 } 126 } 127 [wordBtn setTitle:nil forState:UIControlStateNormal]; 128 } 129 130 131 132 /** 133 * 设置备选答案 134 */ 135 - (void) setCompareAnswer:(JHAppDate *)appDate{ 136 137 /** 清空_wordView中按钮 */ 138 [_wordView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 139 _wordView.userInteractionEnabled = YES; 140 int coclumn = 7; // 设置列数 141 CGFloat selfW = self.view.frame.size.width; // 屏幕的宽度 142 CGFloat marginY =10; 143 CGFloat marginX =7; 144 CGFloat wordBtnW = 35; 145 CGFloat wordBtnH = 35; 146 CGFloat compareBtnMargin =(selfW - coclumn * wordBtnW) / (coclumn +1); 147 for (int i = 0; i < appDate.options.count; i++) { 148 // 求出margin 149 UIButton *compareBtn = [[UIButton alloc] init]; 150 [compareBtn setTag:i]; // 设置按钮的tag 151 CGFloat row = i / coclumn; 152 CGFloat col = i % coclumn; 153 CGFloat oneX = compareBtnMargin + col * (wordBtnW + marginX); 154 CGFloat oneY = compareBtnMargin + row * (wordBtnH + marginY); 155 compareBtn.frame = CGRectMake(oneX, oneY, wordBtnW, wordBtnH); 156 [compareBtn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal]; 157 [compareBtn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted]; 158 [compareBtn setTitle:appDate.options[i] forState:UIControlStateNormal]; 159 [compareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 160 [compareBtn addTarget:self action:@selector(clickBtn:) forControlEvents: UIControlEventTouchUpInside]; 161 [_wordView addSubview:compareBtn]; 162 } 163 } 164 165 /** 166 * 选择答案 167 */ 168 - (void) clickBtn:(UIButton *)compareBtn{ 169 compareBtn.hidden = YES; 170 for (UIButton *clickBtn in _selectWord.subviews) { 171 NSString *str = [clickBtn titleForState:UIControlStateNormal]; 172 if (str.length == 0) { 173 NSString *getStr = [compareBtn titleForState:UIControlStateNormal]; 174 [clickBtn setTitle:getStr forState:UIControlStateNormal]; 175 clickBtn.userInteractionEnabled = YES; 176 break; 177 } 178 } 179 180 // 检查是否填充完毕 181 int count = 0; 182 for (UIButton *answerBtn in _selectWord.subviews) { 183 NSString *answers = [answerBtn titleForState:UIControlStateNormal]; 184 if (answers.length ) { 185 count++; 186 } 187 } 188 if (_selectWord.subviews.count != count) return; 189 _wordView.userInteractionEnabled = NO; 190 191 } 192 193 /** 变化图片 也可以通过模态解决 */ 194 - (void) changeImage{ 195 if (_push == nil) { 196 // 增加蒙蔽 197 UIButton *push = [[UIButton alloc] init]; 198 push.frame = self.view.bounds; 199 push.backgroundColor = [UIColor blackColor]; 200 push.alpha = 0.0; 201 [push addTarget:self action:@selector(changeImage) forControlEvents:UIControlEventTouchUpInside]; 202 203 [self.view insertSubview:push belowSubview:_image]; // 在某层之后插入某层 204 _push = push; 205 // 放大图片 206 [UIView animateWithDuration:0.2 animations:^{ 207 _push.alpha = 0.7; 208 CGFloat h = self.view.frame.size.height; 209 CGFloat w = self.view.frame.size.width; 210 CGRect tempImage = _image.frame; 211 tempImage.origin.x = 0; 212 tempImage.origin.y = (h - w) * 0.5; 213 tempImage.size = CGSizeMake(w, w); 214 _image.frame = tempImage; 215 }]; 216 217 }else{ 218 [UIView animateWithDuration:0.2 animations:^{ 219 // 还原图片 220 _push.alpha = 0.0; 221 _image.frame =CGRectMake(87, 91, 147, 144); 222 } completion:^(BOOL finished) { 223 [_push removeFromSuperview]; 224 _push = nil; 225 }]; 226 227 } 228 229 } 230 231 232 /** 金币总数 */ 233 - (IBAction)tipImage { 234 int coin = [_coinTotal.text intValue]; 235 coin -= 500; 236 NSString *str = [NSString stringWithFormat:@"%d",coin]; 237 _coinTotal.text = str; 238 } 239 240 241 /** 242 * 设置状态栏 243 */ 244 - (UIStatusBarStyle)preferredStatusBarStyle{ 245 return UIStatusBarStyleLightContent; 246 } 247 248 @end
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。