123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- //
- // TempViewController.m
- // jiaPei
- //
- // Created by 张嵘 on 2019/8/27.
- // Copyright © 2019 JCZ. All rights reserved.
- //
- #import "TempViewController.h"
- #import "PracticeExamCell.h"
- #import "QuestionModel.h"
- #import "UIImage+WebP.h"
- #import <AVFoundation/AVFoundation.h> // 基于AVFoundation,通过实例化的控制器来设置player属性
- #import <AVKit/AVKit.h>
- #import <MediaPlayer/MediaPlayer.h>
- @interface TempViewController () <UITableViewDelegate, UITableViewDataSource>
- @property (strong, nonatomic) UILabel *headerLable;///<题目
- @property (strong, nonatomic) UIView *headerView;
- @property (strong, nonatomic) UIImageView *imgView;///<图片
- @property (strong, nonatomic) UITableView *myTableV;
- @property (strong, nonatomic) NSArray *chooseArr;
- @property (strong, nonatomic) NSArray *imgArr;//a,b,c,d 图片 未选择
- @property (strong, nonatomic) NSArray *duiImgArr;//a,b,c,d 图片 多选对的选项没有选
- @property (strong, nonatomic) UIButton *sureBtn;//提交按钮
- @property (strong, nonatomic) UIView *footView;//尾视图
- @property (strong, nonatomic) UIView *quesDetailView;//试题详解
- @property (strong, nonatomic) UILabel *answerLab;//答案
- // @property (strong, nonatomic) MPMoviePlayerController *mpc;//视频
- @end
- @implementation TempViewController
- #pragma mark - Life Circle
- - (void)viewDidLoad {
- [super viewDidLoad];
- _chooseArr = @[@"正确",@"错误"];
- _imgArr =@[@"A",@"B",@"C",@"D"];
- _duiImgArr = @[ @"sel_A",@"sel_B",@"sel_C",@"sel_D"];
- [self.view addSubview:self.myTableV];
- [self creatHeaderView];
- [self creatFootViewSubView];
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark - Table view data source
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- switch (_model.type) {
- case 1:
- return 2;
- break;
-
- default:
- return 4;
- break;
- }
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- PracticeExamCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PracticeExamCell" forIndexPath:indexPath];
- if (_model.isRight!=0||_showAnswer) {
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- }
- cell.item = [self rightOrFalseStyleIndexStr:indexPath];
- return cell;
- }
- /**
- * cell的样式展示判断,0对1错2默认3多选题点后后变色 text的颜色
- *
- * @param indexPath 选中的选项下标
- * @return AnswerItem 返回选项模型
- */
- - (AnswerItem *)rightOrFalseStyleIndexStr:(NSIndexPath *)indexPath {
- AnswerItem *item = [[AnswerItem alloc]init];
- NSString *index = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
- switch (_model.isRight) {//答对答错或没做 0没做 1答对 2答错
- case 0:{
- // 0对1错2默认3多选题点后后变色 text的颜色
- if (_showAnswer) { //背题模式
- if ([_model.RightAwaser containsString:index]) {
- item.picname = @"answer_cell_option_bg_yes";//✔️
- item.type=0;
- }else{
- item.picname = _imgArr[indexPath.row];//多选 该正确答案用户未选择
- item.type=2;
- }
-
- }else{
- if (_model.type==3&&[_model.userAnswer containsString:index]) {//多选
- item.type =3;
- }else{
- item.type =2;
-
- }
- item.picname =_imgArr[indexPath.row];
- }
- }
-
- break;
-
- default:{
-
- if ([_model.RightAwaser containsString:index]) {
- if ([_model.userAnswer containsString:index]) {
- item.picname = @"answer_cell_option_bg_yes";//✔️
-
- }else{
- item.picname = _duiImgArr[indexPath.row];//多选 该正确答案用户未选择
- }
- item.type=0;
- }else if ([_model.userAnswer containsString:index]&&![_model.RightAwaser containsString:index]){ //❌
- item.type=1;
- item.picname = @"answer_cell_option_bg_no";
- }else{
- item.type =2;
- item.picname =_imgArr[indexPath.row];
- }
-
- }
-
- break;
- }
-
-
- switch (_model.type) {
- case 1:{
- item.text = _chooseArr[indexPath.row];
- }
-
- break;
-
- default:{
- item.text = _model.Awasers[indexPath.row];
- }
- break;
- }
- return item;
- }
- /// 获得用户的选项,多选题将用户的选项大小排序 . */
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- if (_model.isRight==0&&!_showAnswer) {
- NSString *index = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
- if (_model.type==3) {//1判断2单选3多选
-
- NSMutableArray *userArray = [NSMutableArray arrayWithArray:[_model.userAnswer componentsSeparatedByString:@","]];
- if ([userArray containsObject:index]) {
- [userArray removeObject:index];
-
- }else{
- [userArray addObject:index];
-
- }
- //多选题,将用户的选项大小排序
- _model.userAnswer = [[userArray sortedArrayUsingSelector:@selector(compare:)] componentsJoinedByString:@","];
-
- [_myTableV reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
-
- }else{
-
- _model.userAnswer = index;
- [self updataTableV];
-
- }
- }
-
- }
- #pragma mark - Private Functions
- /// 创建头视图 _model.type 1判断2单选3多选 . */
- - (void)creatHeaderView {
- WeakSelf(weakSelf)
- //创建headerView
- self.headerView = [UIView new];
- [self.headerView addSubview:self.headerLable];
- UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 40, 20)];
- switch (_model.type) {
- case 1:
- imgV.image = IMG(@"tag-判断");
- break;
- case 2:
- imgV.image = IMG(@"tag-单选");
- break;
- default:
- imgV.image = IMG(@"tag-多选");
- break;
- }
-
- [_headerView addSubview:imgV];
-
- _myTableV.tableHeaderView = self.headerView;
- //设置约束
-
- [self.headerLable mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.mas_equalTo(15);
- make.width.mas_equalTo(kScreenWidth-30);
- }];
-
- [self questionLabStr:[NSString stringWithFormat:@"%d.%@",self.index + 1,_model.question]];
-
- if (_model.category==0) {
- [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.width.mas_equalTo(weakSelf.view.mas_width);
- make.left.top.right.mas_equalTo(0);
- make.bottom.equalTo(weakSelf.headerLable.mas_bottom).offset(15);
- }];
- [_myTableV.tableHeaderView layoutIfNeeded];
- _myTableV.tableHeaderView = self.headerView;
- }else{
- [self creatUIWithType:_model.type];
-
- }
- }
- /// 尾视图 提交答案;权威解释 . */
- - (void)creatFootViewSubView {
- _footView = [UIView new];
- _myTableV.tableFooterView = _footView;
- if (_model.type==3) {//多选
- _sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(30, 15, kScreenWidth-60, 40)];
- [_sureBtn setTitle:@"提交" forState:UIControlStateNormal];
- [_sureBtn addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
- _sureBtn.backgroundColor = defGreen;
- _sureBtn.hidden = YES;
- [_footView addSubview:_sureBtn];
-
- }
- _quesDetailView = [UIView new];
- [_footView addSubview:_quesDetailView];
-
- UIView *leftLine = [[UIView alloc]initWithFrame:CGRectMake(30*HeightScale, 90*HeightScale, kScreenWidth/3, 0.2)];
- [_quesDetailView addSubview:leftLine];
- UIView *rightLine = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth-30*HeightScale-kScreenWidth/3, 90*HeightScale, kScreenWidth/3, 0.2)];
- [_quesDetailView addSubview:rightLine];
- UILabel *titleLab = [UILabel new];
- titleLab.text = @"权威详解";
- titleLab.font = FONT(18);
- [_quesDetailView addSubview:titleLab];
- [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
- make.height.mas_equalTo(15);
- make.top.mas_equalTo(80*HeightScale);
- make.centerX.equalTo(_footView.mas_centerX);
- }];
- _answerLab = [UILabel new];
- NSMutableString *anserStr = [[NSMutableString alloc]initWithString:@"答案:"];
- if ([_model.RightAwaser containsString:@"1"]) {
- [anserStr appendString:@"A"];
- }
- if ([_model.RightAwaser containsString:@"2"]) {
- [anserStr appendString:@"B"];
- }
- if ([_model.RightAwaser containsString:@"3"]) {
- [anserStr appendString:@"C"];
- }
- if ([_model.RightAwaser containsString:@"4"]) {
- [anserStr appendString:@"D"];
- }
- _answerLab.text = anserStr;
- [_quesDetailView addSubview:_answerLab];
- [_answerLab mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.height.mas_equalTo(15);
- make.top.mas_equalTo(160*HeightScale);
- }];
-
- UILabel *difficultLab = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth-180, 160*HeightScale, 160, 15)];
- difficultLab.textAlignment = NSTextAlignmentRight;
- difficultLab.text=[NSString stringWithFormat:@"难度: %zd 颗星",_model.difficulty];
- [_quesDetailView addSubview:difficultLab];
-
- UILabel *contentLab = [UILabel new];
- contentLab.numberOfLines = 0;
- contentLab.text = _model.Comments.length>0?[NSString stringWithFormat:@"%@",[_model.Comments stringByReplacingOccurrencesOfString:@"\\n" withString:@" \r\n"]]:@"";
- [_quesDetailView addSubview:contentLab];
- [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(15);
- make.top.mas_equalTo(260*HeightScale);
- make.right.mas_equalTo(-15);
- }];
- [_quesDetailView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.left.right.mas_equalTo(0);
- make.bottom.equalTo(contentLab.mas_bottom).offset(15);
- }];
- titleLab.textColor =difficultLab.textColor = _answerLab.textColor =leftLine.backgroundColor=rightLine.backgroundColor=RGB_COLOR(88,92,95);
- _quesDetailView.hidden = YES;
- [self reloadFootView];
- }
- /// 这道题没有做过,不自动显示解析,多选的情况下显示提交按钮 . */
- - (void)reloadFootView {
-
- if (_model.isRight==0&&!_showAnswer) {//没做过
- if (_model.type==3) {//多选
- _sureBtn.hidden = NO;
- _quesDetailView.hidden = YES;
- _footView.frame = CGRectMake(0, 0, kScreenWidth, 70);
- }
- }else{
- _sureBtn.hidden = YES;
- _quesDetailView.hidden = NO;
- [_quesDetailView layoutIfNeeded];
- _footView.frame =CGRectMake(0, 0, kScreenWidth, _quesDetailView.frame.size.height);
- }
- _myTableV.tableFooterView = _footView;
- }
- /**
- * 题目页面展示
- *
- * @param type 是否有图片0无1图片2视频
- */
- - (void)creatUIWithType:(NSInteger)type {
- WeakSelf(weakSelf)
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if (_model.category==1) {
- NSString *createPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"/image"]];
- NSString *webPPath = [createPath stringByAppendingPathComponent:_model.picName];
-
- if (![fileManager fileExistsAtPath:webPPath]) {
- if(![fileManager fileExistsAtPath:createPath]){
- [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];
- }
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- NSData *dataImage=[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",@"https://www.dyhoa.com/misc/assets/image/",_model.picName]]];
-
- dispatch_async(dispatch_get_main_queue(), ^{
- if(dataImage){
- self.imgView.image = [UIImage sd_imageWithWebPData:dataImage];
-
- [dataImage writeToFile:webPPath atomically:YES];
-
- }else{
- weakSelf.imgView.image = PLACEHOLDERIMG;
-
- }
- });
- });
- } else {
-
- NSData *dataImage=[NSData dataWithContentsOfFile:webPPath];
- self.imgView.image = [UIImage sd_imageWithWebPData:dataImage];
- }
- [self makeConstraintsImageView];
-
- }else if (_model.category==2) {
-
- // 视频
- // NSString *createPath = IWAccountFile(@"/mp4");
- // NSString *webPPath = [createPath stringByAppendingPathComponent:_model.VediaName];
- //
- // NSURL *mpUrl;
- // if([fileManager fileExistsAtPath:webPPath]){//文件存在
- // mpUrl = [NSURL fileURLWithPath:webPPath];
- //
- //
- // }else{
- // mpUrl =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",Mp4PathUrl,_model.VediaName]];
- //
- // }
- // _mpc = [[MPMoviePlayerController alloc]init];
- // [_headerView addSubview:_mpc.view];
- //
- // _mpc.contentURL = mpUrl;
- // [_mpc prepareToPlay];
- // //视频显示位置
- // [_mpc.view mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.width.mas_equalTo(kwidth-30);
- // make.left.mas_equalTo(15);
- // make.top.equalTo(weakSelf.headerLable.mas_bottom).offset(15);
- // make.height.mas_equalTo(180);
- // }];
- //
- // _mpc.repeatMode = MPMovieRepeatModeOne;
- // _mpc.scalingMode=MPMovieScalingModeAspectFit;
- //
- // //设置控制模式
- // [_mpc setControlStyle:MPMovieControlStyleNone];
- //
- //
- // [_mpc play];
- // [weakSelf.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
- // make.left.top.right.mas_equalTo(0);
- // make.bottom.equalTo(_mpc.view.mas_bottom).offset(15);
- // }];
- // [_myTableV.tableHeaderView layoutIfNeeded];
- // _myTableV.tableHeaderView = self.headerView;
- }
- }
- - (void)makeConstraintsImageView {
- WeakSelf(weakSelf)
- [weakSelf.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(weakSelf.headerLable.mas_bottom).offset(15);
- make.left.mas_equalTo(30);
- make.width.mas_equalTo(kScreenWidth-60);
- make.height.mas_equalTo(kScreenWidth*0.4);
- }];
- [weakSelf.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.width.mas_equalTo(weakSelf.view.mas_width);
- make.left.top.right.mas_equalTo(0);
- make.bottom.equalTo(weakSelf.imgView.mas_bottom).offset(15);
- }];
- [_myTableV.tableHeaderView layoutIfNeeded];
- _myTableV.tableHeaderView = self.headerView;
-
- }
- /// 多选题提交答案 . */
- - (void)sureAction {
- if (_model.userAnswer.length==0) {
- NSLog(@"-----------选项不能为空---------");
- return;
- }
- _model.userAnswer= [_model.userAnswer stringByReplacingOccurrencesOfString:@"," withString:@""];
- [self updataTableV];
- [self reloadFootView];
- }
- /// 判断是否回答正确 isRight 1对2错 . */
- - (void)updataTableV {
- if ([_model.userAnswer isEqualToString:_model.RightAwaser]) {
- _model.isRight = 1;//对
- }else{
- _model.isRight = 2;//错
- }
- [self reloadFootView];
- [_myTableV reloadData];
- if (self.delegate) {
- [self.delegate saveTheAnswerRecordActionWithQuesTionItem:_model isRight:_model.isRight];
- }
- }
- /**
- * 调整问题文字显示,设置首行缩进,行间距
- *
- * @param labStr 问题内容
- */
- - (void)questionLabStr:(NSString *)labStr{
- UILabel *lab= [UILabel new];
- lab.font = _headerLable.font;
- lab.numberOfLines = 0;
- lab.text = labStr;
- NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labStr];
- NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- [paragraphStyle setFirstLineHeadIndent:45];//首行缩进
- CGSize labsize = [lab sizeThatFits:CGSizeMake(kScreenWidth-50, CGFLOAT_MAX)];
- if (labsize.height>2*_headerLable.font.lineHeight) {
- [paragraphStyle setLineSpacing:10];//调整行间距
- }
- [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labStr length])];
- _headerLable.attributedText = attributedString;
- }
- #pragma mark - LazyLoad
- - (UITableView *)myTableV {
- if (!_myTableV) {
- _myTableV = [UITableView new];
- _myTableV.delegate = self;
- _myTableV.dataSource = self;
- _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);
- _myTableV.estimatedRowHeight = 44;
- _myTableV.rowHeight = UITableViewAutomaticDimension;
- _myTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
- [_myTableV registerClass:[PracticeExamCell class] forCellReuseIdentifier:@"PracticeExamCell"];
- }
- return _myTableV;
- }
- - (UILabel *)headerLable {
-
- if (!_headerLable) {
- _headerLable = [UILabel new];
- _headerLable.numberOfLines = 0;
- }
- return _headerLable;
- }
- - (UIImageView *)imgView {
- if (!_imgView) {
- _imgView = [UIImageView new];
- _imgView.contentMode = UIViewContentModeScaleAspectFit;
- [_headerView addSubview:_imgView];
-
- }
- return _imgView;
- }
- @end
|