/** */ #import "ExerView.h" #import #import "CommentCell.h" #import #import "RQYDTXCQuestionModule.h" @interface ExerView() //@interface ExerView() @property(nonatomic,strong)UILabel* lbl00; @property(nonatomic,strong)UILabel* lbl01; @property(nonatomic,strong)UIImageView* mediaImg; @property(nonatomic,strong)AVPlayerViewController *movie; @property(nonatomic,strong)AVPlayer *player; @property(nonatomic,strong)AVPlayerItem *playerItem; @property(nonatomic,strong)UIButton* playBtn; @property (nonatomic, readwrite, strong) MPPlayerController *mpPlayer; @property (nonatomic, readwrite, strong) ZFPlayerController *zfPlayer; @property (nonatomic, readwrite, strong) UIImageView *containerView; @property(nonatomic,strong)CLButton* btn02; @property(nonatomic,strong)CLButton* btn03; @property(nonatomic,strong)CLButton* btn04; @property(nonatomic,strong)CLButton* btn05; @property(nonatomic,strong)UIButton* btnYes; /**正确答案视图 */ @property(nonatomic,strong)UIView* answerV10; @property(nonatomic,strong)UILabel* lbl11; @property(nonatomic,strong)UILabel* lbl12; @property(nonatomic,strong)UIImageView* imgV13; @property(nonatomic,strong)NSMutableArray* imgs; @property(nonatomic,strong)UILabel* lbl14; /**评论的数据源 */ @property(nonatomic,strong)NSArray* comments; /** */ /**其实下面的内容,可以由headerView,和tableView,footerView组合。 只需要设置tableview的frame即可。 反正子view是添加到父类里面的,无所谓。 */ @property (nonatomic, readwrite, strong) QMUIImagePreviewViewController *imagePreviewViewController; @end /**底部的试题分析的cell的个数最大值。 */ #define cellCntMax 3 @implementation ExerView { NSMutableArray* optBtns; UIView *header; UIButton *footer ,*foldBtn; UITableView *myTableView; CGFloat tableH; // NSInteger answ; } /***控件需要一次性创建。 可是frame需要动态设置 */ -(id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (!self) { return nil; } [self setBackgroundColor:[UIColor whiteColor]]; [self createSubViews]; self.contentSize = SGSizeMake(0, 600); return self; } - (void)dealloc { if (self.zfPlayer) { [self.zfPlayer stop]; self.zfPlayer = nil; } [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem]; } /**每次设置question都需要动态设置frame */ -(void)setSubViewFrame{ @weakify(self) if (self.zfPlayer) { [self.zfPlayer stop]; self.zfPlayer = nil; } CGFloat lbl01Y = 0; CGFloat lbl01X = 20; CGFloat lbl01W = kSize.width - lbl01X*2; CGFloat lbl01H = [_lbl01.text heightForWid:lbl01W Font:myDelegate.optionFont]+20; CGRect frame01 = CGRectMake(lbl01X, lbl01Y, lbl01W, lbl01H); self.lbl01.frame = frame01; CGFloat mediaH = 0; // CGRect frameMe ;//= CGRectZero; /**0:无图 1:图片题 2:动画题 */ if (RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN) { if (RQStringIsEmpty(_ydtQuestionModel.media_url)) { /// 无图 _mediaImg.hidden = YES; // _movie.view.hidden = YES; _containerView.hidden = YES; } else { if ([_ydtQuestionModel.media_url containsString:@".webp"]) { // _movie.view.hidden = YES; _containerView.hidden = YES; _mediaImg.hidden = NO; CGFloat imgMX = 10; CGFloat imgMY = CGRectGetMaxY(frame01); CGFloat imgMW = kSize.width - 20; CGFloat imgMH = imgMW *0.5; CGRect frameMe = CGRectMake(imgMX, imgMY, imgMW, imgMH); _mediaImg.frame = frameMe; NSString *media_name = RQStringIsNotEmpty(_ydtQuestionModel.sinaimg)? _ydtQuestionModel.sinaimg : [_ydtQuestionModel.media_url componentsSeparatedByString:@"/"].lastObject; NSData *imageData = [RQ_YDTQuestion_Module getMediaDataWithMediaName:media_name]; NSString *imageStr = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; UIImage *subImage = [UIImage sd_imageWithData:[NSData dataWithBase64EncodedString:imageStr] scale:1 firstFrameOnly:YES]; if (!RQObjectIsNil(subImage)) { if (RQ_COMMON_MANAGER.is_Online_Media_Image) { [_mediaImg sd_setImageWithURL:[NSURL URLWithString:_ydtQuestionModel.media_url] placeholderImage:subImage]; } else { _mediaImg.image = subImage; } } else { [_mediaImg sd_setImageWithURL:[NSURL URLWithString:_ydtQuestionModel.media_url] placeholderImage:RQWebImagePlaceholder()]; } mediaH = imgMH; } else if ([_ydtQuestionModel.media_url containsString:@".mp4"]) { // _movie.view.hidden = NO; _containerView.hidden = NO; _mediaImg.hidden = YES; CGFloat imgMX = 10; CGFloat imgMY = CGRectGetMaxY(frame01); CGFloat imgMW = kSize.width - 20; CGFloat imgMH = imgMW *0.5; CGRect frameMe = CGRectMake(imgMX, imgMY, imgMW, imgMH); self.containerView.frame = frameMe; @weakify(self) // self.mpPlayer = [[MPPlayerController alloc] initWithContainerView:self.containerView]; NSString *media_name = RQStringIsNotEmpty(_ydtQuestionModel.video_url)? _ydtQuestionModel.video_url : [_ydtQuestionModel.media_url componentsSeparatedByString:@"/"].lastObject; NSData *data = [RQ_YDTQuestion_Module getMediaDataWithMediaName:media_name]; NSURL *moveUrl = [NSURL URLWithString:_ydtQuestionModel.media_url]; if (!RQObjectIsNil(data)) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:media_name]; [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil]; moveUrl = [NSURL fileURLWithPath:path]; if (RQ_COMMON_MANAGER.is_Online_Media_Video) { moveUrl = [NSURL URLWithString:_ydtQuestionModel.media_url]; } } // self.containerView.image = [self videoImageWithvideoURL:moveUrl]; self.zfPlayer = [[ZFPlayerController alloc] initWithPlayerManager:[[ZFAVPlayerManager alloc] init] containerView:self.containerView]; self.zfPlayer.currentPlayerManager.view.backgroundColor = UIColor.clearColor; self.zfPlayer.assetURLs = @[moveUrl]; if (self.index == 1) { [self.zfPlayer playTheIndex:0]; } else if ([self.questions.lastObject isEqual:_ydtQuestionModel] || [self.questions.firstObject isEqual:_ydtQuestionModel]) { [self.zfPlayer playTheIndex:0]; } /// 播放完成 self.zfPlayer.playerDidToEnd = ^(id _Nonnull asset) { @strongify(self) [self.zfPlayer.currentPlayerManager replay]; }; self.zfPlayer.playerPlayTimeChanged = ^(id _Nonnull asset, NSTimeInterval currentTime, NSTimeInterval duration) { NSLog(@"%f---%f",currentTime,duration); }; // self.mpPlayer.playableArray = @[_ydtQuestionModel]; // [self.mpPlayer playWithPlayable:_ydtQuestionModel]; // _movie.view.frame = frameMe; // NSURL *url = [NSURL URLWithString:_ydtQuestionModel.media_url]; // _playerItem = [[AVPlayerItem alloc] initWithURL:url]; // _player = [AVPlayer playerWithPlayerItem:_playerItem]; // _movie.player = _player; // if (_movie.readyForDisplay) { // [_player play]; // } // //添加播放完成通知 // [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(runLoopTheMovie:) name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem]; mediaH = imgMH; } else { } } } else { if ([defUser.car_type isEqualToString:@"2"]) { if (0 == _question.media_type.intValue) { _mediaImg.hidden = YES; _movie.view.hidden = YES; } if (1 == self.question.media_type.intValue) { _movie.view.hidden = YES; _mediaImg.hidden = NO; CGFloat imgMX = 10; CGFloat imgMY = CGRectGetMaxY(frame01); CGFloat imgMW = kSize.width - 20; CGFloat imgMH = imgMW *0.5; CGRect frameMe = CGRectMake(imgMX, imgMY, imgMW, imgMH); _mediaImg.frame = frameMe; if ([_question.media containsString:@".webp"]) { NSString *path = [[NSBundle mainBundle] pathForResource:[[_question.media componentsSeparatedByString:@"."] firstObject] ofType:@"webp"]; _mediaImg.image = [UIImage sd_imageWithWebPData:[NSData dataWithContentsOfFile:path]]; } else { _mediaImg.image = [UIImage imageNamed:_question.media]; } mediaH = imgMH; } if (2 == _question.media_type.intValue) { _movie.view.hidden = NO; _mediaImg.hidden = YES; CGFloat imgMX = 10; CGFloat imgMY = CGRectGetMaxY(frame01); CGFloat imgMW = kSize.width - 20; CGFloat imgMH = imgMW *0.5; CGRect frameMe = CGRectMake(imgMX, imgMY, imgMW, imgMH); _movie.view.frame = frameMe; NSString *file=[[NSBundle mainBundle]pathForResource:_question.media ofType:nil]; NSURL *url=[NSURL fileURLWithPath:file]; _playerItem = [[AVPlayerItem alloc] initWithURL:url]; _player = [AVPlayer playerWithPlayerItem:_playerItem]; _movie.player = _player; if (_movie.readyForDisplay) { [_player play]; } //添加播放完成通知 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(runLoopTheMovie:) name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem]; mediaH = imgMH; } } else { if (RQStringIsEmpty(_ydtQuestionModel.media_url)) { /// 无图 _mediaImg.hidden = YES; // _movie.view.hidden = YES; _containerView.hidden = YES; } else { if ([_ydtQuestionModel.media_url containsString:@".webp"]) { // _movie.view.hidden = YES; _containerView.hidden = YES; _mediaImg.hidden = NO; CGFloat imgMX = 10; CGFloat imgMY = CGRectGetMaxY(frame01); CGFloat imgMW = kSize.width - 20; CGFloat imgMH = imgMW *0.5; CGRect frameMe = CGRectMake(imgMX, imgMY, imgMW, imgMH); _mediaImg.frame = frameMe; NSString *media_name = RQStringIsNotEmpty(_ydtQuestionModel.sinaimg)? _ydtQuestionModel.sinaimg : [_ydtQuestionModel.media_url componentsSeparatedByString:@"/"].lastObject; NSData *imageData = [RQ_YDTQuestion_Module getMediaDataWithMediaName:media_name]; NSString *imageStr = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; // NSData *subImageData = [[NSData alloc] initWithBase64EncodedString:imageStr options:NSDataBase64DecodingIgnoreUnknownCharacters]; UIImage *subImage = [UIImage sd_imageWithData:[NSData dataWithBase64EncodedString:imageStr] scale:1 firstFrameOnly:YES]; if (!RQObjectIsNil(subImage)) { if (RQ_COMMON_MANAGER.is_Online_Media_Image) { [_mediaImg sd_setImageWithURL:[NSURL URLWithString:_ydtQuestionModel.media_url] placeholderImage:subImage]; } else { _mediaImg.image = subImage; } } else { [_mediaImg sd_setImageWithURL:[NSURL URLWithString:_ydtQuestionModel.media_url] placeholderImage:RQWebImagePlaceholder()]; } mediaH = imgMH; } else if ([_ydtQuestionModel.media_url containsString:@".mp4"]) { // _movie.view.hidden = NO; _containerView.hidden = NO; _mediaImg.hidden = YES; CGFloat imgMX = 10; CGFloat imgMY = CGRectGetMaxY(frame01); CGFloat imgMW = kSize.width - 20; CGFloat imgMH = imgMW *0.5; CGRect frameMe = CGRectMake(imgMX, imgMY, imgMW, imgMH); self.containerView.frame = frameMe; @weakify(self) // self.mpPlayer = [[MPPlayerController alloc] initWithContainerView:self.containerView]; NSString *media_name = RQStringIsNotEmpty(_ydtQuestionModel.video_url)? _ydtQuestionModel.video_url : [_ydtQuestionModel.media_url componentsSeparatedByString:@"/"].lastObject; NSData *data = [RQ_YDTQuestion_Module getMediaDataWithMediaName:media_name]; NSURL *moveUrl = [NSURL URLWithString:_ydtQuestionModel.media_url]; if (!RQObjectIsNil(data)) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:media_name]; [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil]; moveUrl = [NSURL fileURLWithPath:path]; if (RQ_COMMON_MANAGER.is_Online_Media_Video) { moveUrl = [NSURL URLWithString:_ydtQuestionModel.media_url]; } } // self.containerView.image = [self videoImageWithvideoURL:moveUrl]; self.zfPlayer = [[ZFPlayerController alloc] initWithPlayerManager:[[ZFAVPlayerManager alloc] init] containerView:self.containerView]; self.zfPlayer.currentPlayerManager.view.backgroundColor = UIColor.clearColor; self.zfPlayer.assetURLs = @[moveUrl]; if (self.index == 1) { [self.zfPlayer playTheIndex:0]; } else if ([self.questions.lastObject isEqual:_ydtQuestionModel] || [self.questions.firstObject isEqual:_ydtQuestionModel]) { [self.zfPlayer playTheIndex:0]; } /// 播放完成 self.zfPlayer.playerDidToEnd = ^(id _Nonnull asset) { @strongify(self) [self.zfPlayer.currentPlayerManager replay]; }; self.zfPlayer.playerPlayTimeChanged = ^(id _Nonnull asset, NSTimeInterval currentTime, NSTimeInterval duration) { NSLog(@"%f---%f",currentTime,duration); }; // self.mpPlayer.playableArray = @[_ydtQuestionModel]; // [self.mpPlayer playWithPlayable:_ydtQuestionModel]; // _movie.view.frame = frameMe; // NSURL *url = [NSURL URLWithString:_ydtQuestionModel.media_url]; // _playerItem = [[AVPlayerItem alloc] initWithURL:url]; // _player = [AVPlayer playerWithPlayerItem:_playerItem]; // _movie.player = _player; // if (_movie.readyForDisplay) { // [_player play]; // } // //添加播放完成通知 // [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(runLoopTheMovie:) name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem]; mediaH = imgMH; } else { } } } } /**A */ CGFloat btn02X =0; CGFloat btn02Y =CGRectGetMaxY(frame01) + mediaH +kCellBd ; CGFloat btn02W =kSize.width; CGFloat btn02H =40.0; CGRect frame02 = CGRectMake(btn02X, btn02Y, btn02W, btn02H); self.btn02.frame = frame02; // CLButton* btn; /**B */ CGFloat btn03X =0; CGFloat btn03Y =CGRectGetMaxY(frame02)+kCellBd; CGFloat btn03W =kSize.width; CGFloat btn03H =40.0; CGRect frame03 = CGRectMake(btn03X, btn03Y, btn03W, btn03H); self.btn03.frame = frame03; /**C */ CGFloat btn04X =0; CGFloat btn04Y =CGRectGetMaxY(frame03)+kCellBd; CGFloat btn04W =kSize.width; CGFloat btn04H =40.0; CGRect frame04 = CGRectMake(btn04X, btn04Y, btn04W, btn04H); self.btn04.frame = frame04; /**D */ CGFloat btn05X =0; CGFloat btn05Y =CGRectGetMaxY(frame04)+kCellBd; CGFloat btn05W =kSize.width; CGFloat btn05H =40.0; CGRect frame05 = CGRectMake(btn05X, btn05Y, btn05W, btn05H); self.btn05.frame = frame05; CGFloat x,y,h,w; x = 80; w = kSize.width - x*2; y = btn05Y + btn05H + 30; h = 55; _btnYes.frame = CGRectMake(x, y, w, h); CGFloat ansVX = 0; CGFloat ansVY = 0; /**如果是判断题 */ if (RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN) { if (_ydtQuestionModel.Type == 1) { ansVY = CGRectGetMaxY(frame03) + kSecHeadHeight; } else { ansVY = CGRectGetMaxY(frame05) + kSecHeadHeight; } } else { if ([defUser.car_type isEqualToString:@"2"]) { if (0 == _question.option_type.intValue) { ansVY = CGRectGetMaxY(frame03) + kSecHeadHeight; }else{ ansVY = CGRectGetMaxY(frame05) + kSecHeadHeight; } } else { if (_ydtQuestionModel.Type == 1) { ansVY = CGRectGetMaxY(frame03) + kSecHeadHeight; } else { ansVY = CGRectGetMaxY(frame05) + kSecHeadHeight; } } } CGFloat ansVW = kSize.width; CGFloat ansVH = 500; CGRect frameAnV = CGRectMake(ansVX, ansVY, ansVW, ansVH); self.answerV10.frame = frameAnV; CGFloat lbl11X = 10; CGFloat lbl11Y = kSecHeadHeight; CGFloat lbl11W = 150; CGFloat lbl11H = 30; CGRect frame11 = CGRectMake(lbl11X, lbl11Y, lbl11W, lbl11H); self.lbl11.text = @"最佳解释"; self.lbl11.frame = frame11; self.lbl11.textColor = defGreen; // UILabel* label; CGFloat img13H = 20; CGFloat img13W = img13H*5; CGFloat img13Y = kSecHeadHeight; CGFloat img13X = kSize.width - img13W - 10; CGRect frame13 = CGRectMake(img13X, img13Y, img13W, img13H); self.imgV13.frame = frame13; // int i=0; // for (UIImageView* iv in _imgs) // { // iv.frame = CGRectMake(img13X + img13W/5*i, img13Y+5, img13H -3, img13H-3); // iv.image = [UIImage imageNamed:@"rankstar0_img.png"]; // i++; // } // CGFloat lbl12W = 60; // CGFloat lbl12H = 30; // CGFloat lbl12X = CGRectGetMinX(frame13) - lbl12W; // CGFloat lbl12Y = kSecHeadHeight; // CGRect frame12 = CGRectMake(lbl12X, lbl12Y, lbl12W, lbl12H); self.lbl12.text = @"难度"; // self.lbl12.frame = frame12; /**解释 */ CGFloat lbl14X = 10; CGFloat lbl14Y = CGRectGetMaxY(frame11) + kSecHeadHeight; CGFloat lbl14W = kSize.width - lbl14X*2; CGFloat lbl14H = [_lbl14.text heightForWid:lbl14W Font:myDelegate.optionFont]+20; CGRect frame14 = CGRectMake(lbl14X, lbl14Y, lbl14W, lbl14H); self.lbl14.frame = frame14; CGFloat currentTop = lbl14Y + lbl14H; [header setFrame:CGRectMake(0, currentTop + 10, kSize.width, 30)]; currentTop +=55; [myTableView setFrame:CGRectMake(0, currentTop , kSize.width, tableH)]; [footer setFrame:CGRectMake(0, currentTop + tableH, kSize.width, 40)]; [myTableView setBackgroundColor:[UIColor clearColor]]; _answerV10.height = footer.bottom; if (_answerV10.hidden) { [self setContentSize:kSize]; }else{ [self setContentSize:CGSizeMake(0,_answerV10.bottom+100)]; } } -(void)createSubViews { _comments = [NSArray array]; UILabel* lbl01 = [[UILabel alloc] init]; lbl01.font = [UIFont scaleSize:myDelegate.optionFont]; lbl01.textColor = [UIColor grayColor]; lbl01.numberOfLines = 0; [self addSubview:lbl01]; self.lbl01 = lbl01; UILabel* lbl00 = [[UILabel alloc] init]; self.lbl00 = lbl00; _mediaImg = [[UIImageView alloc] init]; [_mediaImg setContentMode:UIViewContentModeScaleAspectFit]; [_mediaImg setClipsToBounds:YES]; [_mediaImg setBackgroundColor:[UIColor clearColor]]; [self addSubview:_mediaImg]; //图片题放大 [_mediaImg setUserInteractionEnabled:YES]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(magnifyImage)]; //把手势添加到指定视图上 [_mediaImg addGestureRecognizer:tapGesture]; if (RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN) { [self addSubview:self.containerView]; } else { if ([defUser.car_type isEqualToString:@"2"]) { _movie = [[AVPlayerViewController alloc] init]; _movie.showsPlaybackControls = NO; [self addSubview:_movie.view]; } else { [self addSubview:self.containerView]; } } optBtns = [NSMutableArray array]; /**A */ CLButton* btn02= [[CLButton alloc] init]; btn02.imgN = @"option_A.png"; [optBtns addObject:btn02]; self.btn02 = btn02; btn02.tag = RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? 1 *10000 : ([defUser.car_type isEqualToString:@"2"]? 16 : 1 *10000); [self addSubview:btn02]; [btn02 addTarget:self action:@selector(AAction:) forControlEvents:UIControlEventTouchUpInside]; CLButton* btn; btn = self.btn02; [btn addSelfViewWithRect:CGRectMake(0, 0, kSize.width, .5) Color:KlineColor]; /**B */ CLButton* btn03= [[CLButton alloc] init]; self.btn03 = btn03; btn03.tag = RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? 2 * 10000 : ([defUser.car_type isEqualToString:@"2"]? 32 : 2 * 10000); btn03.imgN = @"option_B.png"; [self addSubview:btn03]; [btn03 addTarget:self action:@selector(AAction:) forControlEvents:UIControlEventTouchUpInside]; [optBtns addObject:btn03]; btn = self.btn03; [btn addSelfViewWithRect:CGRectMake(0, 0, kSize.width, .5) Color:KlineColor]; /**C */ CLButton* btn04= [[CLButton alloc] init]; [btn04 setImage:defaultImg forState:UIControlStateNormal]; self.btn04 = btn04; btn04.tag = RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? 3 * 10000 : ([defUser.car_type isEqualToString:@"2"]? 64 : 3 * 10000); btn04.imgN = @"option_C.png"; [self addSubview:btn04]; [btn04 addTarget:self action:@selector(AAction:) forControlEvents:UIControlEventTouchUpInside]; [optBtns addObject:btn04]; btn = self.btn04; [btn addSelfViewWithRect:CGRectMake(0, 0, kSize.width, .5) Color:KlineColor]; /**D */ CLButton* btn05= [[CLButton alloc] init]; self.btn05 = btn05; btn05.tag = RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? 4 * 10000 : ([defUser.car_type isEqualToString:@"2"]? 128 : 4 * 10000); btn05.imgN = @"option_D.png"; [self addSubview:btn05]; [btn05 addTarget:self action:@selector(AAction:) forControlEvents:UIControlEventTouchUpInside]; [optBtns addObject:btn05]; btn = self.btn05; [btn addSelfViewWithRect:CGRectMake(0, 0, kSize.width, .5) Color:KlineColor]; _btnYes = [[UIButton alloc] init]; [_btnYes setBackgroundColor:KlineColor]; [_btnYes setTitle:@"确定" textColor:subTitleColor font:22 fotState:UIControlStateNormal]; [_btnYes addTarget:self action:@selector(multiOptsYes:) forControlEvents:UIControlEventTouchUpInside]; _btnYes.layer.cornerRadius = 10; [self addSubview:_btnYes]; UIView* view10 = [[UIView alloc] init]; self.answerV10 = view10; [self addSubview:view10]; self.lbl11 = [[UILabel alloc] init]; [self.answerV10 addSubview:self.lbl11]; UILabel* label = self.lbl11; [label addSelfViewWithRect:CGRectMake(-10, -12, kSize.width, 8) Color:KlineColor]; self.lbl12 = [[UILabel alloc] init]; [self.answerV10 addSubview:self.lbl12]; self.imgV13 = [[UIImageView alloc] init]; [self.answerV10 addSubview:self.imgV13]; //解释。label self.lbl14 = [[UILabel alloc] init]; [self.answerV10 addSubview:self.lbl14]; label = self.lbl14; [label setFont:[UIFont scaleSize:myDelegate.optionFont]]; [label addSelfViewWithRect:CGRectMake(-10, -10, kSize.width, .5) Color:KlineColor]; _imgs = [NSMutableArray array]; for (int i=0; i<5; i++) { UIImageView *iv = [[UIImageView alloc] init]; [_imgs addObject:iv]; [self.answerV10 addSubview:iv]; } if (RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN) { } else { myTableView = [[UITableView alloc] init]; [myTableView setDataSource:self]; [myTableView setDelegate:self]; myTableView.separatorInset = UIEdgeInsetsMake(0,70, 0, 10); [self.answerV10 addSubview:myTableView]; tableH = 0; //header UIView* vi = [[UIView alloc] initWithFrame:SGRectMake(0, 0, 320, 25)]; [_answerV10 addSubview:vi]; label = [[UILabel alloc] initWithFrame:SGRectMake(10, 0, 80, 25)]; [label setText:@"试题分析"]; [label setTextColor:contentTextColor]; [vi addSubview:label]; header = vi; [label addSelfViewWithRect:SGRectMake(-10, -10, 320, 7) Color:KlineColor]; [label addSelfViewWithRect:SGRectMake(-10, 26, 320, .5) Color:KlineColor]; /**普通状态显示展开。选定状态显示收起 */ UIButton* btnn; btnn = [[UIButton alloc] initWithFrame:SGRectMake(90, 0, 80, 25)]; [btnn setTitle:@"展开" textColor:defGreen font:NormalFont fotState:UIControlStateNormal]; [btnn setTitle:@"收起" textColor:defGreen font:NormalFont fotState:UIControlStateSelected]; [btnn addTarget:self action:@selector(btnHereClick:) forControlEvents:UIControlEventTouchUpInside]; [vi addSubview:btnn]; [btnn setTag:0]; foldBtn = btnn; btnn = [[UIButton alloc] initWithFrame:SGRectMake(320-90, 2.5, 80, 20)]; [btnn setTitle:@"我来分析" textColor:defGreen font:NormalFont fotState:UIControlStateNormal]; [btnn addTarget:self action:@selector(btnHereClick:) forControlEvents:UIControlEventTouchUpInside]; [btnn setBoardWid:.5 Color:defGreen]; [vi addSubview:btnn]; [btnn setTag:1]; btnn = [[UIButton alloc] init]; [btnn setTitle:@"查看全部学员分析" textColor:defGreen font:NormalFont fotState:UIControlStateNormal]; [btnn addTarget:self action:@selector(btnHereClick:) forControlEvents:UIControlEventTouchUpInside]; [_answerV10 addSubview:btnn]; footer = btnn; [btnn addSelfViewWithRect:SGRectMake(0, 40, 320, .5) Color:KlineColor]; [btnn setTag:2]; [myTableView setHidden:YES]; [footer setHidden:YES]; } } #pragma mark - 接收播放完成的通知 - (void)runLoopTheMovie:(NSNotification *)notification { AVPlayerItem *playerItem = notification.object; [playerItem seekToTime:kCMTimeZero]; [_player play]; } //图片放大 - (void)magnifyImage { if (RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN) { [RQControllerHelper.currentViewController presentViewController:self.imagePreviewViewController animated:YES completion:nil]; } else { if ([defUser.car_type isEqualToString:@"2"]) { [RQ_SHARE_FUNCTION showPhotoBrowserWithDataSource:@[_mediaImg.image] currentIndex:0 isCanSave:NO]; } else { [RQControllerHelper.currentViewController presentViewController:self.imagePreviewViewController animated:YES completion:nil]; } } } - (void)setQuestion:(Question *)question { if(!question){ return; } self.comments = nil; [myTableView reloadData]; [foldBtn setSelected:NO]; [myTableView setHidden:YES]; [footer setHidden:YES]; _btnYes.enabled = NO; [self setContentOffset:CGPointMake(0, 0)]; _playBtn.hidden = YES; self.answerV10.hidden = YES; _question = question; for (UIButton* btn in optBtns) { [btn setSelected:NO]; [btn setBackgroundColor:[UIColor clearColor]]; } switch (question.option_type.intValue) { case 0: self.lbl00.text = @"判断"; self.btn04.hidden = YES; self.btn05.hidden = YES; _btnYes.hidden = YES; break; case 1: self.lbl00.text = @"单选"; self.btn04.hidden = NO; self.btn05.hidden = NO; _btnYes.hidden = YES; break; case 2: self.lbl00.text = @"多选"; self.btn04.hidden = NO; self.btn05.hidden = NO; _btnYes.hidden = NO; break; default: break; } self.lbl01.text = [NSString stringWithFormat:@" %@:%@",self.lbl00.text,question.question]; [self.btn02 setTitle:question.optionA forState:UIControlStateNormal]; [self.btn03 setTitle:question.optionB forState:UIControlStateNormal]; [self.btn04 setTitle:question.optionC forState:UIControlStateNormal]; [self.btn05 setTitle:question.optionD forState:UIControlStateNormal]; self.btn02.hidden = !question.optionA || [question.optionA isEqualToString:@""]; self.btn03.hidden = !question.optionB || [question.optionB isEqualToString:@""]; self.btn04.hidden = !question.optionC || [question.optionC isEqualToString:@""]; self.btn05.hidden = !question.optionD || [question.optionD isEqualToString:@""]; /** 选项字体 一键设置 */ [self.lbl01 setFont:[UIFont scaleSize:myDelegate.optionFont]]; [self.btn02.titleLabel setFont:[UIFont scaleSize:myDelegate.optionFont]]; [self.btn03.titleLabel setFont:[UIFont scaleSize:myDelegate.optionFont]]; [self.btn04.titleLabel setFont:[UIFont scaleSize:myDelegate.optionFont]]; [self.btn05.titleLabel setFont:[UIFont scaleSize:myDelegate.optionFont]]; _btn02.imgN = @"option_A.png"; _btn03.imgN = @"option_B.png"; _btn04.imgN = @"option_C.png"; _btn05.imgN = @"option_D.png"; for (CLButton* btn in optBtns) { [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; } [self.lbl14 setFont:[UIFont scaleSize:myDelegate.optionFont]]; self.lbl14.numberOfLines = 0; self.lbl14.text = _question.explain; [self setSubViewFrame]; } - (void)setYdtQuestionModel:(RQYDTQuestionModel *)ydtQuestionModel { if(!ydtQuestionModel){ return; } self.comments = nil; [myTableView reloadData]; [foldBtn setSelected:NO]; [myTableView setHidden:YES]; [footer setHidden:YES]; _btnYes.enabled = NO; [self setContentOffset:CGPointMake(0, 0)]; _playBtn.hidden = YES; self.answerV10.hidden = YES; _ydtQuestionModel = ydtQuestionModel; for (UIButton* btn in optBtns) { [btn setSelected:NO]; [btn setBackgroundColor:[UIColor clearColor]]; } switch (_ydtQuestionModel.Type) { case 1: self.lbl00.text = @"判断"; self.btn04.hidden = YES; self.btn05.hidden = YES; _btnYes.hidden = YES; break; case 2: self.lbl00.text = @"单选"; self.btn04.hidden = NO; self.btn05.hidden = NO; _btnYes.hidden = YES; break; case 3: self.lbl00.text = @"多选"; self.btn04.hidden = NO; self.btn05.hidden = NO; _btnYes.hidden = NO; break; default: break; } self.lbl01.text = [NSString stringWithFormat:@" %@:%@",self.lbl00.text,_ydtQuestionModel.Question]; [self.btn02 setTitle:(_ydtQuestionModel.Type == 1)? @"正确" : _ydtQuestionModel.An1 forState:UIControlStateNormal]; [self.btn03 setTitle:(_ydtQuestionModel.Type == 1)? @"错误" : _ydtQuestionModel.An2 forState:UIControlStateNormal]; [self.btn04 setTitle:_ydtQuestionModel.An3 forState:UIControlStateNormal]; [self.btn05 setTitle:_ydtQuestionModel.An4 forState:UIControlStateNormal]; // self.btn02.hidden = !ydtQuestionModel.An1 || [ydtQuestionModel.An1 isEqualToString:@""]; // self.btn03.hidden = !ydtQuestionModel.An2 || [ydtQuestionModel.An2 isEqualToString:@""]; self.btn04.hidden = !ydtQuestionModel.An3 || [ydtQuestionModel.An3 isEqualToString:@""]; self.btn05.hidden = !ydtQuestionModel.An4 || [ydtQuestionModel.An4 isEqualToString:@""]; /** 选项字体 一键设置 */ [self.lbl01 setFont:[UIFont scaleSize:myDelegate.optionFont]]; [self.btn02.titleLabel setFont:[UIFont scaleSize:myDelegate.optionFont]]; [self.btn03.titleLabel setFont:[UIFont scaleSize:myDelegate.optionFont]]; [self.btn04.titleLabel setFont:[UIFont scaleSize:myDelegate.optionFont]]; [self.btn05.titleLabel setFont:[UIFont scaleSize:myDelegate.optionFont]]; _btn02.imgN = @"option_A.png"; _btn03.imgN = @"option_B.png"; _btn04.imgN = @"option_C.png"; _btn05.imgN = @"option_D.png"; for (CLButton* btn in optBtns) { [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; } [self.lbl14 setFont:[UIFont scaleSize:myDelegate.optionFont]]; self.lbl14.numberOfLines = 0; self.lbl14.text = _ydtQuestionModel.explain; [self setSubViewFrame]; } //改变背景颜色 实现夜间模式 -(void)setNightMode:(BOOL)isNight { if (isNight) { [self setBackgroundColor:nightColor];//[UIColor orangeColor] } else { [self setBackgroundColor:[UIColor whiteColor]]; } } #pragma mark - -(void)getAllComments { @weakify(self) if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr=[NSMutableArray array]; [arr property:RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? @(_ydtQuestionModel.ID) : ([defUser.car_type isEqualToString:@"2"]? _question._id : @(_ydtQuestionModel.ID)) forKey:@"id"]; [arr property:@"1" forKey:@"isPage"]; [arr property:@"7" forKey:@"pageSize"]; [arr property:@"1" forKey:@"currentPage"]; NSString* method = @"getQuestionComments"; [MBProgressHUD showLoadToView:self]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *dict) { @strongify(self) [MBProgressHUD hideHUDForView:self]; if (!dict) { ShowMsgFailed(); return; } if ([dict[@"code"] isEqualToString:@"1"]) { ShowMsg(dict[@"body"]); return; } _comments = dict[@"body"]; if (![dict[@"body"] isKindOfClass:[NSArray class]]) { _comments = @[]; } if (_comments.count < 1) { ShowMsg(@"暂无相关评论"); } /*题目分析 剩下两个找不到(你晚上看看) PHOTO = ""; "QC_CRDATE" = "2017-01-23 16:08:44"; "QC_DESC" = gdfgfd; "QC_DOWN_NUM" = 0; "QC_ID" = 61; "QC_QID" = 1; "QC_TYPE" = 1; "QC_UP_NUM" = 0; "QC_USER_ID" = 1; */ tableH = 0; for (int i=0; i< _comments.count && i < cellCntMax; i++) { NSDictionary *dic = _comments[i]; tableH += 105 + [dic[@"QC_DESC"] heightForWid:(kSize.width - 80) Font:15]; } // [footer setTitle:[NSString stringWithFormat:@"查看全部%d位学员分析",(int)_comments.count] textColor:defGreen font:NormalFont fotState:UIControlStateNormal]; [myTableView reloadData]; [self setSubViewFrame]; }]; } -(void)showComments:(BOOL)isShow { if (isShow) { }else{ } } -(void)btnHereClick:(UIButton*)sender{ if (0 == sender.tag) { [myTableView setHidden:sender.selected]; [footer setHidden:sender.selected]; sender.selected = !sender.selected; if (sender.selected) { [self getAllComments]; } }else{ [self.delegate2 gotoAllCommentVC]; } } -(void)setShowAnswer:(BOOL)showAnswer { [_answerV10 setHidden:!showAnswer]; } -(void)playMovie { if (RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN) { if (RQStringIsNotEmpty(_ydtQuestionModel.video_url)) { [self.mpPlayer playWithPlayable:_ydtQuestionModel]; } } else { if ([defUser.car_type isEqualToString:@"2"]) { if (_question.media_type.intValue == 2) { [_player play]; } } else { if (RQStringIsNotEmpty(_ydtQuestionModel.video_url)) { [self.mpPlayer playWithPlayable:_ydtQuestionModel]; } } } } -(void)stopMovie { if (RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN) { } else { if ([defUser.car_type isEqualToString:@"2"]) { [_player pause]; } } if (self.zfPlayer) { [self.zfPlayer stop]; self.zfPlayer = nil; } } /**4个按钮用tag来区分。16,32,64,128 当点击按钮后,设置正确答案的img 如果答题错误,把点的按钮变成错误的img 不管是否正确都要跳出正确答案, */ -(void)AAction:(CLButton*)sender { int type = RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? _ydtQuestionModel.Type : ([defUser.car_type isEqualToString:@"2"]? _question.option_type.intValue : _ydtQuestionModel.Type); if (type == (RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? 3 : ([defUser.car_type isEqualToString:@"2"]? 2 : 3))) { sender.selected = !sender.selected; if (sender.selected) { [sender setBackgroundColor:RGBA_COLOR(134, 204, 71, 0.5)];//backGroundColor }else{ [sender setBackgroundColor:[UIColor clearColor]]; } int optCnt = 0; for (int i=0; i<4; i++) { UIButton* btn = optBtns[i]; if (btn.selected) { optCnt++; } } if (optCnt > 1) { _btnYes.enabled = YES; [_btnYes setBackgroundColor:defGreen]; }else{ _btnYes.enabled = NO; [_btnYes setBackgroundColor:KlineColor]; } return; } if ([self.delegate2 respondsToSelector:@selector(didChooseOption:isRight:)]) { [self.delegate2 didChooseOption:sender.tag isRight:RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? (_ydtQuestionModel.AnswerTrue.intValue == (sender.tag / 10000)) : ([defUser.car_type isEqualToString:@"2"]? (_question.answer.intValue == sender.tag) : (_ydtQuestionModel.AnswerTrue.intValue == (sender.tag / 10000)))]; } [self choose:sender.tag]; return; } /**展示答案的权利,交给exVC。这里就不去设置了。 这里主要是根据。opt设置按钮的图片。 */ -(void)choose:(NSInteger)opt { _btn02.enabled = NO; _btn03.enabled = NO; _btn04.enabled = NO; _btn05.enabled = NO; _btnYes.hidden = YES; int type = RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? _ydtQuestionModel.Type : ([defUser.car_type isEqualToString:@"2"]? _question.option_type.intValue : _ydtQuestionModel.Type); if (RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN) { if ( 3 == type) { NSString* answer = _ydtQuestionModel.AnswerTrue; NSString* myAnswer = [NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:opt]]; NSArray* arr = @[@"1",@"2",@"3",@"4"]; for (int i = 0; i < 4; i++) { CLButton* btn = optBtns[i]; NSRange range = [answer rangeOfString:arr[i]]; NSRange myRange = [myAnswer rangeOfString:arr[i]]; BOOL isSel = (myRange.location != NSNotFound); BOOL isRig = (range.location != NSNotFound); if (isSel) { [btn setBackgroundColor:RGBA_COLOR(134, 204, 71, 0.5)]; }else{ [btn setBackgroundColor:[UIColor clearColor]]; } if (isRig) { btn.imgN = @"question_correct.png"; }else if (isSel){ btn.imgN = @"question_error.png"; } } return; } for (CLButton* btn in optBtns) { if (_ydtQuestionModel.AnswerTrue.intValue == (btn.tag / 10000)) { btn.imgN = @"question_correct.png"; [btn setTitleColor:defGreen forState:UIControlStateNormal]; } } if ((opt / 10000) != _ydtQuestionModel.AnswerTrue.intValue) { switch (opt / 10000) { case 1: _btn02.imgN = @"question_error.png"; break; case 2: _btn03.imgN = @"question_error.png"; break; case 3: _btn04.imgN = @"question_error.png"; break; case 4: _btn05.imgN = @"question_error.png"; break; default: break; } } } else { if ([defUser.car_type isEqualToString:@"2"]) { if ( 2 == type) { NSString* answer = [Util getAnswerByTen:_question.answer.intValue]; NSString* myAnswer = [Util getAnswerByTen:opt]; NSArray* arr = @[@"A",@"B",@"C",@"D"]; for (int i = 0; i < 4; i++) { CLButton* btn = optBtns[i]; NSRange range = [answer rangeOfString:arr[i]]; NSRange myRange = [myAnswer rangeOfString:arr[i]]; BOOL isSel = (myRange.location != NSNotFound); BOOL isRig = (range.location != NSNotFound); if (isSel) { [btn setBackgroundColor:RGBA_COLOR(134, 204, 71, 0.5)];//klinecolor }else{ [btn setBackgroundColor:[UIColor clearColor]]; } if (isRig) { btn.imgN = @"question_correct.png"; }else if (isSel){ btn.imgN = @"question_error.png"; } } return; } for (CLButton* btn in optBtns) { if (_question.answer.intValue == btn.tag) { btn.imgN = @"question_correct.png"; [btn setTitleColor:defGreen forState:UIControlStateNormal]; } } if (opt != _question.answer.intValue) { switch (opt) { case 16: _btn02.imgN = @"question_error.png"; break; case 32: _btn03.imgN = @"question_error.png"; break; case 64: _btn04.imgN = @"question_error.png"; break; case 128: _btn05.imgN = @"question_error.png"; break; default: break; } } } else { if ( 3 == type) { NSString* answer = _ydtQuestionModel.AnswerTrue; NSString* myAnswer = [NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:opt]]; NSArray* arr = @[@"1",@"2",@"3",@"4"]; for (int i = 0; i < 4; i++) { CLButton* btn = optBtns[i]; NSRange range = [answer rangeOfString:arr[i]]; NSRange myRange = [myAnswer rangeOfString:arr[i]]; BOOL isSel = (myRange.location != NSNotFound); BOOL isRig = (range.location != NSNotFound); if (isSel) { [btn setBackgroundColor:RGBA_COLOR(134, 204, 71, 0.5)]; }else{ [btn setBackgroundColor:[UIColor clearColor]]; } if (isRig) { btn.imgN = @"question_correct.png"; }else if (isSel){ btn.imgN = @"question_error.png"; } } return; } for (CLButton* btn in optBtns) { if (_ydtQuestionModel.AnswerTrue.intValue == (btn.tag / 10000)) { btn.imgN = @"question_correct.png"; [btn setTitleColor:defGreen forState:UIControlStateNormal]; } } if ((opt / 10000) != _ydtQuestionModel.AnswerTrue.intValue) { switch (opt / 10000) { case 1: _btn02.imgN = @"question_error.png"; break; case 2: _btn03.imgN = @"question_error.png"; break; case 3: _btn04.imgN = @"question_error.png"; break; case 4: _btn05.imgN = @"question_error.png"; break; default: break; } } } } } -(void)multiOptsYes:(UIButton*)sender { NSInteger answ = 0; NSArray* arr = RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? @[@"1",@"2",@"3",@"4"] : ([defUser.car_type isEqualToString:@"2"]? @[@"16",@"32",@"64",@"128"] : @[@"1",@"2",@"3",@"4"]); NSMutableString *str = [NSMutableString string]; for (int i=0; i<4; i++) { UIButton* btn = optBtns[i]; if (btn.selected) { [str appendString:arr[i]]; answ += [arr[i] integerValue]; } } if ([self.delegate2 respondsToSelector:@selector(didChooseOption:isRight:)]) { [self.delegate2 didChooseOption:RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? str.integerValue : ([defUser.car_type isEqualToString:@"2"]? answ : str.integerValue) isRight:RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? (_ydtQuestionModel.AnswerTrue.intValue == str.integerValue) : ([defUser.car_type isEqualToString:@"2"]? (_question.answer.intValue == answ) : (_ydtQuestionModel.AnswerTrue.intValue == str.integerValue))]; } [self choose:RQ_COMMON_MANAGER.YYXC_SERVICE_OPEN? str.integerValue : ([defUser.car_type isEqualToString:@"2"]? answ : str.integerValue)]; } /** 使用方法:首先是setQuestion。然后是setAnswer 0要恢复按钮点击。 16-128要调用choose,表示做过了。 这里也要判断一下。是单选还是多选。 */ -(void)setAnswer:(NSInteger)answer { if (0 == answer) { _btn02.enabled = YES; _btn03.enabled = YES; _btn04.enabled = YES; _btn05.enabled = YES; _btnYes.enabled = NO; [_btnYes setBackgroundColor:KlineColor]; return; } [self choose:answer]; return; } #pragma mark - -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _comments.count > cellCntMax ? cellCntMax : _comments.count; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSDictionary *dic = _comments[indexPath.row]; return 110 + [dic[@"QC_DESC"] heightForWid:(kSize.width - 80) Font:15]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CommentCell* cell = [CommentCell cellForTableView:tableView Style:CommentCellStyleQuestion]; [cell setCommentDic:_comments[indexPath.row]]; return cell; } - (UIImage*)videoImageWithvideoURL:(NSURL *)videoURL { NSString *cacheKeyStr = [[YYWebImageManager sharedManager] cacheKeyForURL:[NSURL URLWithString:videoURL.absoluteString]]; UIImage *memoryImage = [[YYImageCache sharedCache] getImageForKey:cacheKeyStr]; if (memoryImage) { return memoryImage; } else { UIImage *thumbnailImage = [self firstFrameWithVideoURL:videoURL size:CGSizeMake(RQ_SCREEN_WIDTH - 32.f, RQ_SCREEN_HEIGHT - RQ_APPLICATION_TOP_BAR_HEIGHT - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT)]; dispatch_async(dispatch_get_main_queue(), ^{ YYImageCache * cache = [YYImageCache sharedCache]; [cache setImage:thumbnailImage forKey:videoURL.absoluteString]; }); return thumbnailImage; } } // 获取视频第一帧 - (UIImage*)firstFrameWithVideoURL:(NSURL*)url size:(CGSize)size { NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey]; AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:url options:opts]; AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset]; generator.appliesPreferredTrackTransform = YES; generator.maximumSize = CGSizeMake(size.width, size.height); NSError*error =nil; CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(0, 10) actualTime:NULL error:&error]; { return [UIImage imageWithCGImage:img]; } return nil; } #pragma mark - - (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView { return 1; } - (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index { zoomImageView.reusedIdentifier = @(index); // 模拟异步加载的情况 if (index == 2) { [zoomImageView showLoading]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if ([zoomImageView.reusedIdentifier isEqual:@(index)]) { [zoomImageView hideEmptyView]; zoomImageView.image = _mediaImg.image; zoomImageView.contentMode = UIViewContentModeScaleAspectFill; zoomImageView.maximumZoomScale = 3.f; } }); } else { zoomImageView.image = _mediaImg.image; } } - (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index { return QMUIImagePreviewMediaTypeImage; } #pragma mark - - (void)singleTouchInZoomingImageView:(QMUIZoomImageView *)zoomImageView location:(CGPoint)location { // 退出图片预览 [RQControllerHelper.currentViewController dismissViewControllerAnimated:YES completion:^{ }]; } #pragma mark - LazyLoad - (UIImageView *)containerView { if (!_containerView) { _containerView = [[UIImageView alloc] qmui_initWithSize:CGSizeMake(RQ_SCREEN_WIDTH - 20, (RQ_SCREEN_WIDTH - 20) / 2.f)]; _containerView.backgroundColor = UIColor.blackColor; UILabel *loadingLabel = [[UILabel alloc] qmui_initWithFont:RQRegularFont(25) textColor:RQ_MAIN_TEXT_COLOR_WHITE]; loadingLabel.text = @"画面加载中..."; [_containerView addSubview:loadingLabel]; [loadingLabel mas_remakeConstraints:^(MASConstraintMaker *make) { make.center.mas_equalTo(_containerView); }]; } return _containerView; } - (void)setIndex:(NSInteger)index { _index = index; } - (QMUIImagePreviewViewController *)imagePreviewViewController { @weakify(self) if (!_imagePreviewViewController) { _imagePreviewViewController = [[QMUIImagePreviewViewController alloc] init]; _imagePreviewViewController.presentingStyle = QMUIImagePreviewViewControllerTransitioningStyleZoom;// 将 present 动画改为 zoom,也即从某个位置放大到屏幕中央。默认样式为 fade。 _imagePreviewViewController.imagePreviewView.delegate = self;// 将内部的图片查看器 delegate 指向当前 viewController,以获取要查看的图片数据 // 当需要在退出大图预览时做一些事情的时候,可配合 UIViewController (QMUI) 的 qmui_visibleStateDidChangeBlock 来实现。 _imagePreviewViewController.qmui_visibleStateDidChangeBlock = ^(QMUIImagePreviewViewController *viewController, QMUIViewControllerVisibleState visibleState) { if (visibleState == QMUIViewControllerWillDisappear) { // NSInteger exitAtIndex = viewController.imagePreviewView.currentImageIndex; } }; self.imagePreviewViewController.sourceImageView = ^UIView *{ @strongify(self) return self.mediaImg; }; } return _imagePreviewViewController; } @end