TempViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. //
  2. // TempViewController.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2019/8/27.
  6. // Copyright © 2019 JCZ. All rights reserved.
  7. //
  8. #import "TempViewController.h"
  9. #import "PracticeExamCell.h"
  10. #import "QuestionModel.h"
  11. #import "UIImage+WebP.h"
  12. #import <AVFoundation/AVFoundation.h> // 基于AVFoundation,通过实例化的控制器来设置player属性
  13. #import <AVKit/AVKit.h>
  14. #import <MediaPlayer/MediaPlayer.h>
  15. @interface TempViewController () <UITableViewDelegate, UITableViewDataSource>
  16. @property (strong, nonatomic) UILabel *headerLable;///<题目
  17. @property (strong, nonatomic) UIView *headerView;
  18. @property (strong, nonatomic) UIImageView *imgView;///<图片
  19. @property (strong, nonatomic) UITableView *myTableV;
  20. @property (strong, nonatomic) NSArray *chooseArr;
  21. @property (strong, nonatomic) NSArray *imgArr;//a,b,c,d 图片 未选择
  22. @property (strong, nonatomic) NSArray *duiImgArr;//a,b,c,d 图片 多选对的选项没有选
  23. @property (strong, nonatomic) UIButton *sureBtn;//提交按钮
  24. @property (strong, nonatomic) UIView *footView;//尾视图
  25. @property (strong, nonatomic) UIView *quesDetailView;//试题详解
  26. @property (strong, nonatomic) UILabel *answerLab;//答案
  27. // @property (strong, nonatomic) MPMoviePlayerController *mpc;//视频
  28. @end
  29. @implementation TempViewController
  30. #pragma mark - Life Circle
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. _chooseArr = @[@"正确",@"错误"];
  34. _imgArr =@[@"A",@"B",@"C",@"D"];
  35. _duiImgArr = @[ @"sel_A",@"sel_B",@"sel_C",@"sel_D"];
  36. [self.view addSubview:self.myTableV];
  37. [self creatHeaderView];
  38. [self creatFootViewSubView];
  39. }
  40. - (void)dealloc {
  41. [[NSNotificationCenter defaultCenter] removeObserver:self];
  42. }
  43. #pragma mark - Table view data source
  44. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  45. switch (_model.type) {
  46. case 1:
  47. return 2;
  48. break;
  49. default:
  50. return 4;
  51. break;
  52. }
  53. }
  54. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  55. PracticeExamCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PracticeExamCell" forIndexPath:indexPath];
  56. if (_model.isRight!=0||_showAnswer) {
  57. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  58. }
  59. cell.item = [self rightOrFalseStyleIndexStr:indexPath];
  60. return cell;
  61. }
  62. /**
  63. * cell的样式展示判断,0对1错2默认3多选题点后后变色 text的颜色
  64. *
  65. * @param indexPath 选中的选项下标
  66. * @return AnswerItem 返回选项模型
  67. */
  68. - (AnswerItem *)rightOrFalseStyleIndexStr:(NSIndexPath *)indexPath {
  69. AnswerItem *item = [[AnswerItem alloc]init];
  70. NSString *index = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
  71. switch (_model.isRight) {//答对答错或没做 0没做 1答对 2答错
  72. case 0:{
  73. // 0对1错2默认3多选题点后后变色 text的颜色
  74. if (_showAnswer) { //背题模式
  75. if ([_model.RightAwaser containsString:index]) {
  76. item.picname = @"answer_cell_option_bg_yes";//✔️
  77. item.type=0;
  78. }else{
  79. item.picname = _imgArr[indexPath.row];//多选 该正确答案用户未选择
  80. item.type=2;
  81. }
  82. }else{
  83. if (_model.type==3&&[_model.userAnswer containsString:index]) {//多选
  84. item.type =3;
  85. }else{
  86. item.type =2;
  87. }
  88. item.picname =_imgArr[indexPath.row];
  89. }
  90. }
  91. break;
  92. default:{
  93. if ([_model.RightAwaser containsString:index]) {
  94. if ([_model.userAnswer containsString:index]) {
  95. item.picname = @"answer_cell_option_bg_yes";//✔️
  96. }else{
  97. item.picname = _duiImgArr[indexPath.row];//多选 该正确答案用户未选择
  98. }
  99. item.type=0;
  100. }else if ([_model.userAnswer containsString:index]&&![_model.RightAwaser containsString:index]){ //❌
  101. item.type=1;
  102. item.picname = @"answer_cell_option_bg_no";
  103. }else{
  104. item.type =2;
  105. item.picname =_imgArr[indexPath.row];
  106. }
  107. }
  108. break;
  109. }
  110. switch (_model.type) {
  111. case 1:{
  112. item.text = _chooseArr[indexPath.row];
  113. }
  114. break;
  115. default:{
  116. item.text = _model.Awasers[indexPath.row];
  117. }
  118. break;
  119. }
  120. return item;
  121. }
  122. /// 获得用户的选项,多选题将用户的选项大小排序 . */
  123. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  124. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  125. if (_model.isRight==0&&!_showAnswer) {
  126. NSString *index = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
  127. if (_model.type==3) {//1判断2单选3多选
  128. NSMutableArray *userArray = [NSMutableArray arrayWithArray:[_model.userAnswer componentsSeparatedByString:@","]];
  129. if ([userArray containsObject:index]) {
  130. [userArray removeObject:index];
  131. }else{
  132. [userArray addObject:index];
  133. }
  134. //多选题,将用户的选项大小排序
  135. _model.userAnswer = [[userArray sortedArrayUsingSelector:@selector(compare:)] componentsJoinedByString:@","];
  136. [_myTableV reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  137. }else{
  138. _model.userAnswer = index;
  139. [self updataTableV];
  140. }
  141. }
  142. }
  143. #pragma mark - Private Functions
  144. /// 创建头视图 _model.type 1判断2单选3多选 . */
  145. - (void)creatHeaderView {
  146. WeakSelf(weakSelf)
  147. //创建headerView
  148. self.headerView = [UIView new];
  149. [self.headerView addSubview:self.headerLable];
  150. UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 40, 20)];
  151. switch (_model.type) {
  152. case 1:
  153. imgV.image = IMG(@"tag-判断");
  154. break;
  155. case 2:
  156. imgV.image = IMG(@"tag-单选");
  157. break;
  158. default:
  159. imgV.image = IMG(@"tag-多选");
  160. break;
  161. }
  162. [_headerView addSubview:imgV];
  163. _myTableV.tableHeaderView = self.headerView;
  164. //设置约束
  165. [self.headerLable mas_makeConstraints:^(MASConstraintMaker *make) {
  166. make.left.top.mas_equalTo(15);
  167. make.width.mas_equalTo(kScreenWidth-30);
  168. }];
  169. [self questionLabStr:[NSString stringWithFormat:@"%d.%@",self.index + 1,_model.question]];
  170. if (_model.category==0) {
  171. [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
  172. make.width.mas_equalTo(weakSelf.view.mas_width);
  173. make.left.top.right.mas_equalTo(0);
  174. make.bottom.equalTo(weakSelf.headerLable.mas_bottom).offset(15);
  175. }];
  176. [_myTableV.tableHeaderView layoutIfNeeded];
  177. _myTableV.tableHeaderView = self.headerView;
  178. }else{
  179. [self creatUIWithType:_model.type];
  180. }
  181. }
  182. /// 尾视图 提交答案;权威解释 . */
  183. - (void)creatFootViewSubView {
  184. _footView = [UIView new];
  185. _myTableV.tableFooterView = _footView;
  186. if (_model.type==3) {//多选
  187. _sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(30, 15, kScreenWidth-60, 40)];
  188. [_sureBtn setTitle:@"提交" forState:UIControlStateNormal];
  189. [_sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
  190. _sureBtn.backgroundColor = defGreen;
  191. _sureBtn.hidden = YES;
  192. [_footView addSubview:_sureBtn];
  193. }
  194. _quesDetailView = [UIView new];
  195. [_footView addSubview:_quesDetailView];
  196. UIView *leftLine = [[UIView alloc]initWithFrame:CGRectMake(30*HeightScale, 90*HeightScale, kScreenWidth/3, 0.2)];
  197. [_quesDetailView addSubview:leftLine];
  198. UIView *rightLine = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth-30*HeightScale-kScreenWidth/3, 90*HeightScale, kScreenWidth/3, 0.2)];
  199. [_quesDetailView addSubview:rightLine];
  200. UILabel *titleLab = [UILabel new];
  201. titleLab.text = @"权威详解";
  202. titleLab.font = FONT(18);
  203. [_quesDetailView addSubview:titleLab];
  204. [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
  205. make.height.mas_equalTo(15);
  206. make.top.mas_equalTo(80*HeightScale);
  207. make.centerX.equalTo(_footView.mas_centerX);
  208. }];
  209. _answerLab = [UILabel new];
  210. NSMutableString *anserStr = [[NSMutableString alloc]initWithString:@"答案:"];
  211. if ([_model.RightAwaser containsString:@"1"]) {
  212. [anserStr appendString:@"A"];
  213. }
  214. if ([_model.RightAwaser containsString:@"2"]) {
  215. [anserStr appendString:@"B"];
  216. }
  217. if ([_model.RightAwaser containsString:@"3"]) {
  218. [anserStr appendString:@"C"];
  219. }
  220. if ([_model.RightAwaser containsString:@"4"]) {
  221. [anserStr appendString:@"D"];
  222. }
  223. _answerLab.text = anserStr;
  224. [_quesDetailView addSubview:_answerLab];
  225. [_answerLab mas_makeConstraints:^(MASConstraintMaker *make) {
  226. make.left.height.mas_equalTo(15);
  227. make.top.mas_equalTo(160*HeightScale);
  228. }];
  229. UILabel *difficultLab = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth-180, 160*HeightScale, 160, 15)];
  230. difficultLab.textAlignment = NSTextAlignmentRight;
  231. difficultLab.text=[NSString stringWithFormat:@"难度: %zd 颗星",_model.difficulty];
  232. [_quesDetailView addSubview:difficultLab];
  233. UILabel *contentLab = [UILabel new];
  234. contentLab.numberOfLines = 0;
  235. contentLab.text = _model.Comments.length>0?[NSString stringWithFormat:@"%@",[_model.Comments stringByReplacingOccurrencesOfString:@"\\n" withString:@" \r\n"]]:@"";
  236. [_quesDetailView addSubview:contentLab];
  237. [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
  238. make.left.mas_equalTo(15);
  239. make.top.mas_equalTo(260*HeightScale);
  240. make.right.mas_equalTo(-15);
  241. }];
  242. [_quesDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
  243. make.top.left.right.mas_equalTo(0);
  244. make.bottom.equalTo(contentLab.mas_bottom).offset(15);
  245. }];
  246. titleLab.textColor =difficultLab.textColor = _answerLab.textColor =leftLine.backgroundColor=rightLine.backgroundColor=RGB_COLOR(88,92,95);
  247. _quesDetailView.hidden = YES;
  248. [self reloadFootView];
  249. }
  250. /// 这道题没有做过,不自动显示解析,多选的情况下显示提交按钮 . */
  251. - (void)reloadFootView {
  252. if (_model.isRight==0&&!_showAnswer) {//没做过
  253. if (_model.type==3) {//多选
  254. _sureBtn.hidden = NO;
  255. _quesDetailView.hidden = YES;
  256. _footView.frame = CGRectMake(0, 0, kScreenWidth, 70);
  257. }
  258. }else{
  259. _sureBtn.hidden = YES;
  260. _quesDetailView.hidden = NO;
  261. [_quesDetailView layoutIfNeeded];
  262. _footView.frame =CGRectMake(0, 0, kScreenWidth, _quesDetailView.frame.size.height);
  263. }
  264. _myTableV.tableFooterView = _footView;
  265. }
  266. /**
  267. * 题目页面展示
  268. *
  269. * @param type 是否有图片0无1图片2视频
  270. */
  271. - (void)creatUIWithType:(NSInteger)type {
  272. WeakSelf(weakSelf)
  273. NSFileManager *fileManager = [NSFileManager defaultManager];
  274. if (_model.category==1) {
  275. NSString *createPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"/image"]];
  276. NSString *webPPath = [createPath stringByAppendingPathComponent:_model.picName];
  277. if (![fileManager fileExistsAtPath:webPPath]) {
  278. if(![fileManager fileExistsAtPath:createPath]){
  279. [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
  280. }
  281. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  282. NSData *dataImage=[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",@"https://www.dyhoa.com/misc/assets/image/",_model.picName]]];
  283. dispatch_async(dispatch_get_main_queue(), ^{
  284. if(dataImage){
  285. self.imgView.image = [UIImage sd_imageWithWebPData:dataImage];
  286. [dataImage writeToFile:webPPath atomically:YES];
  287. }else{
  288. weakSelf.imgView.image = PLACEHOLDERIMG;
  289. }
  290. });
  291. });
  292. } else {
  293. NSData *dataImage=[NSData dataWithContentsOfFile:webPPath];
  294. self.imgView.image = [UIImage sd_imageWithWebPData:dataImage];
  295. }
  296. [self makeConstraintsImageView];
  297. }else if (_model.category==2) {
  298. // 视频
  299. // NSString *createPath = IWAccountFile(@"/mp4");
  300. // NSString *webPPath = [createPath stringByAppendingPathComponent:_model.VediaName];
  301. //
  302. // NSURL *mpUrl;
  303. // if([fileManager fileExistsAtPath:webPPath]){//文件存在
  304. // mpUrl = [NSURL fileURLWithPath:webPPath];
  305. //
  306. //
  307. // }else{
  308. // mpUrl =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",Mp4PathUrl,_model.VediaName]];
  309. //
  310. // }
  311. // _mpc = [[MPMoviePlayerController alloc]init];
  312. // [_headerView addSubview:_mpc.view];
  313. //
  314. // _mpc.contentURL = mpUrl;
  315. // [_mpc prepareToPlay];
  316. // //视频显示位置
  317. // [_mpc.view mas_makeConstraints:^(MASConstraintMaker *make) {
  318. // make.width.mas_equalTo(kwidth-30);
  319. // make.left.mas_equalTo(15);
  320. // make.top.equalTo(weakSelf.headerLable.mas_bottom).offset(15);
  321. // make.height.mas_equalTo(180);
  322. // }];
  323. //
  324. // _mpc.repeatMode = MPMovieRepeatModeOne;
  325. // _mpc.scalingMode=MPMovieScalingModeAspectFit;
  326. //
  327. // //设置控制模式
  328. // [_mpc setControlStyle:MPMovieControlStyleNone];
  329. //
  330. //
  331. // [_mpc play];
  332. // [weakSelf.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
  333. // make.left.top.right.mas_equalTo(0);
  334. // make.bottom.equalTo(_mpc.view.mas_bottom).offset(15);
  335. // }];
  336. // [_myTableV.tableHeaderView layoutIfNeeded];
  337. // _myTableV.tableHeaderView = self.headerView;
  338. }
  339. }
  340. - (void)makeConstraintsImageView {
  341. WeakSelf(weakSelf)
  342. [weakSelf.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
  343. make.top.equalTo(weakSelf.headerLable.mas_bottom).offset(15);
  344. make.left.mas_equalTo(30);
  345. make.width.mas_equalTo(kScreenWidth-60);
  346. make.height.mas_equalTo(kScreenWidth*0.4);
  347. }];
  348. [weakSelf.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
  349. make.width.mas_equalTo(weakSelf.view.mas_width);
  350. make.left.top.right.mas_equalTo(0);
  351. make.bottom.equalTo(weakSelf.imgView.mas_bottom).offset(15);
  352. }];
  353. [_myTableV.tableHeaderView layoutIfNeeded];
  354. _myTableV.tableHeaderView = self.headerView;
  355. }
  356. /// 多选题提交答案 . */
  357. - (void)sureAction {
  358. if (_model.userAnswer.length==0) {
  359. NSLog(@"-----------选项不能为空---------");
  360. return;
  361. }
  362. _model.userAnswer= [_model.userAnswer stringByReplacingOccurrencesOfString:@"," withString:@""];
  363. [self updataTableV];
  364. [self reloadFootView];
  365. }
  366. /// 判断是否回答正确 isRight 1对2错 . */
  367. - (void)updataTableV {
  368. if ([_model.userAnswer isEqualToString:_model.RightAwaser]) {
  369. _model.isRight = 1;//对
  370. }else{
  371. _model.isRight = 2;//错
  372. }
  373. [self reloadFootView];
  374. [_myTableV reloadData];
  375. if (self.delegate) {
  376. [self.delegate saveTheAnswerRecordActionWithQuesTionItem:_model isRight:_model.isRight];
  377. }
  378. }
  379. /**
  380. * 调整问题文字显示,设置首行缩进,行间距
  381. *
  382. * @param labStr 问题内容
  383. */
  384. - (void)questionLabStr:(NSString *)labStr{
  385. UILabel *lab= [UILabel new];
  386. lab.font = _headerLable.font;
  387. lab.numberOfLines = 0;
  388. lab.text = labStr;
  389. NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labStr];
  390. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
  391. [paragraphStyle setFirstLineHeadIndent:45];//首行缩进
  392. CGSize labsize = [lab sizeThatFits:CGSizeMake(kScreenWidth-50, CGFLOAT_MAX)];
  393. if (labsize.height>2*_headerLable.font.lineHeight) {
  394. [paragraphStyle setLineSpacing:10];//调整行间距
  395. }
  396. [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labStr length])];
  397. _headerLable.attributedText = attributedString;
  398. }
  399. #pragma mark - LazyLoad
  400. - (UITableView *)myTableV {
  401. if (!_myTableV) {
  402. _myTableV = [UITableView new];
  403. _myTableV.delegate = self;
  404. _myTableV.dataSource = self;
  405. _myTableV.frame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.x, self.view.bounds.size.width, self.view.bounds.size.height - 44 - kSafeAreaBottomHeight);
  406. _myTableV.estimatedRowHeight = 44;
  407. _myTableV.rowHeight = UITableViewAutomaticDimension;
  408. _myTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
  409. [_myTableV registerClass:[PracticeExamCell class] forCellReuseIdentifier:@"PracticeExamCell"];
  410. }
  411. return _myTableV;
  412. }
  413. - (UILabel *)headerLable {
  414. if (!_headerLable) {
  415. _headerLable = [UILabel new];
  416. _headerLable.numberOfLines = 0;
  417. }
  418. return _headerLable;
  419. }
  420. - (UIImageView *)imgView {
  421. if (!_imgView) {
  422. _imgView = [UIImageView new];
  423. _imgView.contentMode = UIViewContentModeScaleAspectFit;
  424. [_headerView addSubview:_imgView];
  425. }
  426. return _imgView;
  427. }
  428. @end