PCSPreviewViewController.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. //
  2. // PCSPreviewViewController.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by lr on 2023/3/11.
  6. //
  7. #import "PCSPreviewViewController.h"
  8. #import <Masonry/Masonry.h>
  9. #import "QuitMultipleModeAlertViewController.h"
  10. #import "UIImage+name.h"
  11. #import <AVFoundation/AVFoundation.h>
  12. #import <AVKit/AVKit.h>
  13. @interface PCSPreCollectionCell : UICollectionViewCell
  14. @property (nonatomic) LenzResourceItemModel *model;
  15. @property (nonatomic) UIImageView *imageView;
  16. @property (nonatomic) UIImageView *videoImageView;
  17. @property (nonatomic) AVPlayerViewController *player;
  18. @property (nonatomic) BOOL isPlayer;
  19. @end
  20. @implementation PCSPreCollectionCell
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. if (self = [super initWithFrame:frame]) {
  23. self.contentView.backgroundColor = [UIColor clearColor];
  24. [self.contentView addSubview:self.imageView];
  25. [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  26. make.edges.mas_offset(0);
  27. }];
  28. [self.contentView addSubview:self.player.view];
  29. [self.contentView addSubview:self.videoImageView];
  30. [self.videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  31. make.center.mas_equalTo(self.contentView);
  32. make.width.height.mas_offset(48);
  33. }];
  34. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
  35. }
  36. return self;
  37. }
  38. - (void)setModel:(LenzResourceItemModel *)model {
  39. _model = model;
  40. self.imageView.image = model.previewImage;
  41. if (model.mode == SDK_CAPTURE_MODE_MOVIE) {
  42. self.videoImageView.hidden = NO;
  43. } else {
  44. self.videoImageView.hidden = YES;
  45. }
  46. }
  47. - (void)setIsPlayer:(BOOL)isPlayer {
  48. if (isPlayer) {
  49. AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:_model.path]];
  50. self.player.player = player;
  51. [self.player.player play];
  52. self.player.view.hidden = NO;
  53. self.videoImageView.hidden = YES;
  54. } else {
  55. [self.player.player pause];
  56. self.player.view.hidden = YES;
  57. self.videoImageView.hidden = NO;
  58. }
  59. }
  60. - (void)playerAction {
  61. if (self.player.player.timeControlStatus == AVPlayerTimeControlStatusPlaying) {
  62. [self.player.player pause];
  63. self.videoImageView.hidden = NO;
  64. } else {
  65. [self.player.player play];
  66. self.videoImageView.hidden = YES;
  67. }
  68. }
  69. - (void)layoutSubviews {
  70. [super layoutSubviews];
  71. self.player.view.frame = self.contentView.bounds;
  72. }
  73. - (UIImageView *)imageView {
  74. if (!_imageView) {
  75. _imageView = [[UIImageView alloc]init];
  76. _imageView.layer.cornerRadius = 8;
  77. _imageView.layer.masksToBounds = YES;
  78. // _imageView.backgroundColor = [UIColor blackColor];
  79. _imageView.contentMode = UIViewContentModeScaleAspectFit;
  80. }
  81. return _imageView;
  82. }
  83. - (UIImageView *)videoImageView {
  84. if (!_videoImageView) {
  85. _videoImageView = [[UIImageView alloc]init];
  86. _videoImageView.image = [UIImage loadNamed:@"icon_video"];
  87. _videoImageView.hidden = YES;
  88. }
  89. return _videoImageView;
  90. }
  91. - (AVPlayerViewController *)player {
  92. if (!_player) {
  93. _player = [[AVPlayerViewController alloc]init];
  94. _player.view.hidden = YES;
  95. _player.view.backgroundColor = [UIColor clearColor];
  96. _player.videoGravity = AVLayerVideoGravityResizeAspect;
  97. _player.showsPlaybackControls = NO;
  98. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(playerAction)];
  99. [_player.view addGestureRecognizer:tap];
  100. }
  101. return _player;
  102. }
  103. - (void)playbackFinished {
  104. if (_model.mode == SDK_CAPTURE_MODE_MOVIE) {
  105. self.videoImageView.hidden = NO;
  106. self.player.view.hidden = YES;
  107. }
  108. }
  109. @end
  110. @interface PCSPreviewViewController ()<UIScrollViewDelegate, AVPlayerViewControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
  111. @property (nonatomic) UILabel *countLabel;
  112. @property (nonatomic) UIView *modeView;
  113. @property (nonatomic) UIScrollView *scrollView;
  114. @property (nonatomic) UILabel *curentLabel;
  115. @property (nonatomic) UIView *bottomView;
  116. @property (nonatomic) UIButton *backButton;
  117. @property (nonatomic) UILabel *backLabel;
  118. @property (nonatomic) UIButton *deleteButton;
  119. @property (nonatomic) UILabel *deleteLabel;
  120. @property (nonatomic) UIImageView *lineImageView;
  121. @property (nonatomic) NSInteger currentIndex;
  122. @property (nonatomic) UIImageView *videoImageView;
  123. @property (nonatomic) AVPlayerViewController *lastPlayer;
  124. //@property (nonatomic) NSInteger currentModeDataIndex;
  125. @property (nonatomic) NSMutableArray <AVPlayerViewController *> *allPlayer;
  126. @property (nonatomic) UICollectionView* collectionView;
  127. @property (nonatomic) NSIndexPath *playerIndexPath;
  128. @property (nonatomic) NSIndexPath *currentIndexPath;
  129. @end
  130. @implementation PCSPreviewViewController
  131. - (void)dealloc {
  132. }
  133. - (void)viewDidLoad {
  134. [super viewDidLoad];
  135. self.allPlayer = [NSMutableArray array];
  136. self.view.backgroundColor = [UIColor colorWithRed:60/255.0 green:58/255.0 blue:61/255.0 alpha:1];
  137. [self.view addSubview:self.countLabel];
  138. CGFloat top = UIApplication.sharedApplication.delegate.window.safeAreaInsets.top;
  139. [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  140. make.top.mas_offset(top + 20);
  141. make.right.mas_offset(-20);
  142. make.height.mas_offset(44);
  143. }];
  144. [self.view addSubview:self.modeView];
  145. [self.modeView mas_makeConstraints:^(MASConstraintMaker *make) {
  146. make.left.mas_offset(0);
  147. make.top.mas_equalTo(self.countLabel);
  148. make.height.mas_equalTo(self.countLabel);
  149. make.right.mas_equalTo(self.countLabel.mas_left).mas_offset(-10);
  150. }];
  151. [self setupModeView];
  152. [self.view addSubview:self.bottomView];
  153. [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
  154. make.left.right.bottom.mas_offset(0);
  155. make.height.mas_offset(150);
  156. }];
  157. [self.bottomView addSubview:self.backLabel];
  158. [self.backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  159. make.bottom.mas_offset(-30);
  160. make.centerX.mas_equalTo(self.bottomView).mas_offset(-60);
  161. }];
  162. [self.bottomView addSubview:self.backButton];
  163. [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
  164. make.bottom.mas_equalTo(self.backLabel.mas_top).mas_offset(-20);
  165. make.centerX.mas_equalTo(self.backLabel);
  166. make.width.height.mas_offset(60);
  167. }];
  168. [self.bottomView addSubview:self.deleteLabel];
  169. [self.deleteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  170. make.bottom.mas_offset(-30);
  171. make.centerX.mas_equalTo(self.bottomView).mas_offset(60);
  172. }];
  173. [self.bottomView addSubview:self.deleteButton];
  174. [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
  175. make.bottom.mas_equalTo(self.deleteLabel.mas_top).mas_offset(-20);
  176. make.centerX.mas_equalTo(self.deleteLabel);
  177. make.width.height.mas_offset(60);
  178. }];
  179. UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc]init];
  180. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  181. layout.minimumLineSpacing = 0;
  182. layout.minimumInteritemSpacing = 0;
  183. layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - top - 224);
  184. self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
  185. self.collectionView.delegate = self;
  186. self.collectionView.dataSource = self;
  187. self.collectionView.pagingEnabled = YES;
  188. self.collectionView.backgroundColor = [UIColor clearColor];
  189. self.collectionView.hidden = YES;
  190. // 注册item类型
  191. [self.collectionView registerClass:[PCSPreCollectionCell class] forCellWithReuseIdentifier:@"PCSPreCollectionCell"];
  192. [self.view addSubview:self.collectionView];
  193. [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  194. make.left.mas_offset(20);
  195. make.right.mas_offset(-20);
  196. make.top.mas_equalTo(self.countLabel.mas_bottom).mas_offset(10);
  197. make.bottom.mas_equalTo(self.bottomView.mas_top);
  198. }];
  199. self.currentIndexPath = nil;
  200. if (self.model.continousArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_CONTINUOUS) {
  201. self.currentIndex = 1;
  202. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.continousArray.count - 1 inSection:0];
  203. }
  204. if (self.model.movieArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_MOVIE) {
  205. self.currentIndex = 2;
  206. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.movieArray.count - 1 inSection:1];
  207. }
  208. if (self.model.panoramArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_PANORAMA) {
  209. self.currentIndex = 3;
  210. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.panoramArray.count - 1 inSection:2];
  211. }
  212. if (self.model.aiPanoramArray.count > 0 && self.selectIndex == SDK_CAPTURE_MODE_INTELLEGENCE_PANORAMA) {
  213. self.currentIndex = 4;
  214. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.aiPanoramArray.count - 1 inSection:3];
  215. }
  216. if(!self.currentIndexPath) {
  217. if (self.model.aiPanoramArray.count > 0) {
  218. self.currentIndex = 4;
  219. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.aiPanoramArray.count - 1 inSection:3];
  220. } else if (self.model.panoramArray.count > 0) {
  221. self.currentIndex = 3;
  222. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.panoramArray.count - 1 inSection:2];
  223. } else if (self.model.movieArray.count > 0) {
  224. self.currentIndex = 2;
  225. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.movieArray.count - 1 inSection:1];
  226. } else {
  227. self.currentIndex = 1;
  228. self.currentIndexPath = [NSIndexPath indexPathForItem:self.model.continousArray.count - 1 inSection:0];
  229. }
  230. }
  231. [self changeLabelStatusWith:self.currentIndex];
  232. [self updateCountLabelWith:self.currentIndexPath];
  233. [self.view addSubview:self.curentLabel];
  234. [self.curentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  235. make.centerX.mas_equalTo(self.view);
  236. make.width.mas_offset(100);
  237. make.height.mas_offset(32);
  238. make.bottom.mas_equalTo(self.collectionView).mas_offset(16);
  239. }];
  240. }
  241. - (void)viewDidAppear:(BOOL)animated {
  242. [super viewDidAppear:animated];
  243. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  244. self.collectionView.hidden = NO;
  245. UICollectionViewLayoutAttributes*attributes = [self.collectionView layoutAttributesForItemAtIndexPath:self.currentIndexPath];
  246. CGRect rect = attributes.frame;
  247. [self.collectionView setContentOffset:CGPointMake(rect.origin.x, rect.origin.y ) animated:NO];
  248. });
  249. }
  250. - (UIImageView *)lineImageView {
  251. if (!_lineImageView) {
  252. _lineImageView = [[UIImageView alloc]init];
  253. _lineImageView.image = [UIImage loadNamed:@"icon_preview_line"];
  254. }
  255. return _lineImageView;
  256. }
  257. - (void)updateCountLabelWith:(NSIndexPath *)indexPath {
  258. if (!indexPath) {
  259. return;
  260. }
  261. if (indexPath.section == 0) {
  262. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.continousArray.count];
  263. } else if (indexPath.section == 1) {
  264. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld条", self.currentIndexPath.row + 1, self.model.movieArray.count];
  265. } else if (indexPath.section == 2) {
  266. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.panoramArray.count];
  267. } else {
  268. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.aiPanoramArray.count];
  269. }
  270. }
  271. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  272. return 4;
  273. }
  274. //返回每个分区的item个数
  275. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  276. if (section == 0) {
  277. return self.model.continousArray.count;
  278. } else if (section == 1) {
  279. return self.model.movieArray.count;
  280. } else if (section == 2) {
  281. return self.model.panoramArray.count;
  282. } else {
  283. return self.model.aiPanoramArray.count;
  284. }
  285. }
  286. //返回每个item
  287. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  288. PCSPreCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PCSPreCollectionCell" forIndexPath:indexPath];
  289. cell.isPlayer = NO;
  290. if (indexPath.section == 0) {
  291. if (self.model.continousArray.count > indexPath.row) {
  292. LenzResourceItemModel *model = self.model.continousArray[indexPath.row];
  293. cell.model = model;
  294. }
  295. } else if (indexPath.section == 1) {
  296. if (self.model.movieArray.count > indexPath.row) {
  297. LenzResourceItemModel *model = self.model.movieArray[indexPath.row];
  298. cell.model = model;
  299. cell.isPlayer = indexPath == self.playerIndexPath;
  300. }
  301. } else if (indexPath.section == 2) {
  302. if (self.model.panoramArray.count > indexPath.row) {
  303. LenzResourceItemModel *model = self.model.panoramArray[indexPath.row];
  304. cell.model = model;
  305. }
  306. } else if (indexPath.section == 3) {
  307. if (self.model.aiPanoramArray.count > indexPath.row) {
  308. LenzResourceItemModel *model = self.model.aiPanoramArray[indexPath.row];
  309. cell.model = model;
  310. }
  311. }
  312. return cell;
  313. }
  314. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  315. if (indexPath.section == 1) {
  316. self.playerIndexPath = indexPath;
  317. [self.collectionView reloadData];
  318. }
  319. }
  320. // 监听UIScrollView的滑动停止
  321. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  322. self.playerIndexPath = nil;
  323. NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
  324. NSIndexPath *currentIndexPath = indexPaths.firstObject;
  325. self.currentIndexPath = currentIndexPath;
  326. [self changeLabelStatusWith:currentIndexPath.section + 1];
  327. [self updateCountLabelWith:currentIndexPath];
  328. }
  329. - (void)updateCurrentIndexPath {
  330. NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
  331. NSIndexPath *currentIndexPath = indexPaths.firstObject;
  332. self.currentIndexPath = currentIndexPath;
  333. [self changeLabelStatusWith:currentIndexPath.section + 1];
  334. [self updateCountLabelWith:currentIndexPath];
  335. }
  336. - (void)updateViewWhenDelete {
  337. self.playerIndexPath = nil;
  338. NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
  339. NSIndexPath *currentIndexPath = indexPaths.firstObject;
  340. self.currentIndexPath = currentIndexPath;
  341. [self changeLabelStatusWith:currentIndexPath.section + 1];
  342. [self updateCountLabelWith:currentIndexPath];
  343. }
  344. - (void)setupModeView {
  345. [self.modeView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  346. CGFloat margin = [UIScreen mainScreen].bounds.size.width == 375 ? 20 : 30;
  347. UIView *lastView = nil;
  348. NSInteger count = 0;
  349. self.currentIndex = 0;
  350. if (self.model.continousArray.count) {
  351. count += self.model.continousArray.count;
  352. self.currentIndex = 1;
  353. UILabel *label = [[UILabel alloc]init];
  354. label.text = @"连拍";
  355. label.tag = 1;
  356. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  357. [label addGestureRecognizer:tap];
  358. label.userInteractionEnabled = YES;
  359. label.textColor = [UIColor whiteColor];
  360. [self.modeView addSubview:label];
  361. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  362. make.left.mas_offset(20);
  363. make.centerY.mas_equalTo(self.countLabel);
  364. }];
  365. lastView = label;
  366. }
  367. if (self.model.movieArray.count) {
  368. count += self.model.movieArray.count;
  369. if (self.currentIndex == 0) {
  370. self.currentIndex = 2;
  371. }
  372. UILabel *label = [[UILabel alloc]init];
  373. label.text = @"视频";
  374. label.tag = 2;
  375. label.textColor = [UIColor whiteColor];
  376. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  377. [label addGestureRecognizer:tap];
  378. label.userInteractionEnabled = YES;
  379. [self.modeView addSubview:label];
  380. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  381. if (lastView) {
  382. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  383. } else {
  384. make.left.mas_equalTo(20);
  385. }
  386. make.centerY.mas_equalTo(self.countLabel);
  387. }];
  388. lastView = label;
  389. }
  390. if (self.model.panoramArray.count) {
  391. count += self.model.panoramArray.count;
  392. if (self.currentIndex == 0) {
  393. self.currentIndex = 3;
  394. }
  395. UILabel *label = [[UILabel alloc]init];
  396. label.text = @"全景";
  397. label.tag = 3;
  398. label.textColor = [UIColor whiteColor];
  399. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  400. [label addGestureRecognizer:tap];
  401. label.userInteractionEnabled = YES;
  402. [self.modeView addSubview:label];
  403. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  404. if (lastView) {
  405. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  406. } else {
  407. make.left.mas_equalTo(20);
  408. }
  409. make.centerY.mas_equalTo(self.countLabel);
  410. }];
  411. lastView = label;
  412. }
  413. if (self.model.aiPanoramArray.count) {
  414. count += self.model.aiPanoramArray.count;
  415. if (self.currentIndex == 0) {
  416. self.currentIndex = 4;
  417. }
  418. UILabel *label = [[UILabel alloc]init];
  419. label.text = @"智能全景";
  420. label.tag = 4;
  421. label.textColor = [UIColor whiteColor];
  422. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  423. [label addGestureRecognizer:tap];
  424. label.userInteractionEnabled = YES;
  425. [self.modeView addSubview:label];
  426. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  427. if (lastView) {
  428. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  429. } else {
  430. make.left.mas_equalTo(20);
  431. }
  432. make.centerY.mas_equalTo(self.countLabel);
  433. }];
  434. lastView = label;
  435. }
  436. self.countLabel.text = [NSString stringWithFormat:@"共%ld笔数据", count];
  437. [self.modeView addSubview:self.lineImageView];
  438. }
  439. - (void)updateTotal {
  440. NSInteger count = 0;
  441. if (self.model.continousArray.count) {
  442. count += self.model.continousArray.count;
  443. }
  444. if (self.model.movieArray.count) {
  445. count += self.model.movieArray.count;
  446. }
  447. if (self.model.panoramArray.count) {
  448. count += self.model.panoramArray.count;
  449. }
  450. if (self.model.aiPanoramArray.count) {
  451. count += self.model.aiPanoramArray.count;
  452. }
  453. self.countLabel.text = [NSString stringWithFormat:@"共%ld笔数据", count];
  454. }
  455. - (void)changeAction:(UITapGestureRecognizer *)tap {
  456. [self.view layoutIfNeeded];
  457. UILabel *label = (UILabel *)tap.view;
  458. self.currentIndexPath = [NSIndexPath indexPathForItem:0 inSection:label.tag - 1];
  459. UICollectionViewLayoutAttributes*attributes = [self.collectionView layoutAttributesForItemAtIndexPath:self.currentIndexPath];
  460. CGRect rect = attributes.frame;
  461. [self.collectionView setContentOffset:CGPointMake(rect.origin.x, rect.origin.y ) animated:NO];
  462. [self changeLabelStatusWith:label.tag];
  463. [self updateCountLabelWith:self.currentIndexPath];
  464. }
  465. - (void)changeLabelStatusWith:(NSInteger)index {
  466. if(index <=0) {
  467. return;
  468. }
  469. UILabel *label1 = [self.modeView viewWithTag:1];
  470. if (label1) {
  471. label1.textColor = [UIColor whiteColor];
  472. }
  473. UILabel *label2 = [self.modeView viewWithTag:2];
  474. if (label2) {
  475. label2.textColor = [UIColor whiteColor];
  476. }
  477. UILabel *label3 = [self.modeView viewWithTag:3];
  478. if (label3) {
  479. label3.textColor = [UIColor whiteColor];
  480. }
  481. UILabel *label4 = [self.modeView viewWithTag:4];
  482. if (label4) {
  483. label4.textColor = [UIColor whiteColor];
  484. }
  485. UILabel *label = (UILabel *)[self.modeView viewWithTag:index];
  486. if (label) {
  487. label.textColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  488. [self.lineImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  489. make.top.mas_equalTo(label.mas_bottom);
  490. make.centerX.mas_equalTo(label);
  491. make.width.mas_offset(18);
  492. make.height.mas_offset(9);
  493. }];
  494. }
  495. self.currentIndex = index;
  496. }
  497. - (void)updateScrollWith:(NSInteger)tag {
  498. if (tag > 0) {
  499. NSArray <LenzResourceItemModel *> *source = nil;
  500. if (tag == 1) {
  501. source = self.model.continousArray;
  502. } else if (tag == 2) {
  503. source = self.model.movieArray;
  504. } else if (tag == 3) {
  505. source = self.model.panoramArray;
  506. } else if (tag == 4) {
  507. source = self.model.aiPanoramArray;
  508. }
  509. [self updateScrollViewWith:source];
  510. }
  511. }
  512. - (void)updateScrollViewWith:(NSArray <LenzResourceItemModel *> *)array {
  513. CGFloat width = CGRectGetWidth(self.scrollView.frame);
  514. CGFloat height = CGRectGetHeight(self.scrollView.frame);
  515. [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  516. self.scrollView.pagingEnabled = YES;
  517. self.scrollView.contentSize = CGSizeMake(width * array.count, height);
  518. __block UIImageView *lastImageView = nil;
  519. [array enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  520. UIImageView *imageView = [[UIImageView alloc]init];
  521. imageView.image = obj.image;
  522. imageView.tag = idx;
  523. [self.scrollView addSubview:imageView];
  524. [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  525. make.top.mas_offset(0);
  526. make.height.mas_offset(height);
  527. make.width.mas_offset(width);
  528. if (lastImageView) {
  529. make.left.mas_equalTo(lastImageView.mas_right);
  530. } else {
  531. make.left.mas_offset(0);
  532. }
  533. }];
  534. UIImageView *videoImageView = [[UIImageView alloc]init];
  535. videoImageView.image = [UIImage loadNamed:@"icon_video"];
  536. [imageView addSubview:videoImageView];
  537. [videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  538. make.center.mas_equalTo(imageView);
  539. make.width.height.mas_offset(48);
  540. }];
  541. [self.view setNeedsLayout];
  542. [self.view layoutIfNeeded];
  543. if (obj.mode == SDK_CAPTURE_MODE_MOVIE) {
  544. videoImageView.hidden = NO;
  545. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(videoAction:)];
  546. [imageView addGestureRecognizer:tap];
  547. imageView.userInteractionEnabled = YES;
  548. AVPlayerViewController *player = [[AVPlayerViewController alloc]init];
  549. player.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
  550. player.view.hidden = YES;
  551. [imageView addSubview:player.view];
  552. [self.allPlayer addObject:player];
  553. } else {
  554. videoImageView.hidden = YES;
  555. }
  556. lastImageView = imageView;
  557. }];
  558. self.curentLabel.text = [NSString stringWithFormat:@"第%d/%ld条", 1, array.count];
  559. }
  560. - (void)videoAction:(UITapGestureRecognizer *)tap {
  561. UIImageView *view = (UIImageView *)tap.view;
  562. if (self.model.movieArray.count > view.tag) {
  563. LenzResourceItemModel *model = self.model.movieArray[view.tag];
  564. AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:model.path]];
  565. if (self.lastPlayer) {
  566. [self.lastPlayer.player pause];
  567. self.lastPlayer.player = nil;
  568. self.lastPlayer.view.hidden = YES;
  569. }
  570. AVPlayerViewController *playerVC = self.allPlayer[view.tag];
  571. playerVC.view.hidden = NO;
  572. playerVC.player = player;
  573. [playerVC.player play];
  574. self.lastPlayer = playerVC;
  575. }
  576. }
  577. - (BOOL)shouldAutorotate {
  578. return NO;
  579. }
  580. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  581. return UIInterfaceOrientationMaskPortrait;
  582. }
  583. - (void)backAction {
  584. [self dismissViewControllerAnimated:YES completion:nil];
  585. }
  586. - (void)deleteAction {
  587. NSString *title = @"图片删除后无法恢复,请确认!";
  588. if (self.currentIndex == 2) {
  589. title = @"视频删除后无法恢复,请确认!";
  590. }
  591. [QuitMultipleModeAlertViewController show:self title:@"确认提醒" text:title leftBtnTitle:@"取消" rightBtnTitle:@"确定" withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  592. [alertController dismissViewControllerAnimated:YES completion:nil];
  593. } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  594. NSIndexPath *showIndexPath = nil;
  595. if (self.currentIndex == 1) {
  596. if (self.model.continousArray.count > self.currentIndexPath.item) {
  597. [self.model.continousArray removeObjectAtIndex:self.currentIndexPath.item];
  598. }
  599. if (self.model.continousArray.count > 0 ) {
  600. showIndexPath = [NSIndexPath indexPathForRow:self.currentIndexPath.item - 1 inSection:self.currentIndexPath.section];
  601. }
  602. } else if (self.currentIndex == 2) {
  603. if (self.model.movieArray.count > self.currentIndexPath.item) {
  604. [self.model.movieArray removeObjectAtIndex:self.currentIndexPath.item];
  605. }
  606. if (self.model.movieArray.count > 0 ) {
  607. showIndexPath = [NSIndexPath indexPathForRow:self.currentIndexPath.item - 1 inSection:self.currentIndexPath.section];
  608. }
  609. } else if (self.currentIndex == 3) {
  610. if (self.model.panoramArray.count > self.currentIndexPath.item) {
  611. [self.model.panoramArray removeObjectAtIndex:self.currentIndexPath.item];
  612. }
  613. if (self.model.panoramArray.count > 0 ) {
  614. showIndexPath = [NSIndexPath indexPathForRow:self.currentIndexPath.item - 1 inSection:self.currentIndexPath.section];
  615. }
  616. } else if (self.currentIndex == 4) {
  617. if (self.model.aiPanoramArray.count > self.currentIndexPath.item) {
  618. [self.model.aiPanoramArray removeObjectAtIndex:self.currentIndexPath.item];
  619. }
  620. if (self.model.aiPanoramArray.count > 0 ) {
  621. showIndexPath = [NSIndexPath indexPathForRow:self.currentIndexPath.item - 1 inSection:self.currentIndexPath.section];
  622. }
  623. }
  624. if(showIndexPath) {
  625. UICollectionViewLayoutAttributes*attributes = [self.collectionView layoutAttributesForItemAtIndexPath:showIndexPath];
  626. CGRect rect = attributes.frame;
  627. [self.collectionView reloadData];
  628. [self.collectionView setContentOffset:CGPointMake(rect.origin.x, rect.origin.y ) animated:NO];
  629. } else {
  630. [self setupModeView];
  631. [self scrollViewDidEndDecelerating:self.collectionView];
  632. [self.collectionView reloadData];
  633. }
  634. [self.view setNeedsLayout];
  635. [self.view layoutIfNeeded];
  636. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  637. [self updateCurrentIndexPath];
  638. });
  639. [self updateTotal];
  640. [self updateViewWhenDelete];
  641. if (self.dataChangeBlock) {
  642. self.dataChangeBlock();
  643. }
  644. if (self.model.continousArray.count == 0 &&
  645. self.model.movieArray.count == 0 &&
  646. self.model.panoramArray.count == 0 &&
  647. self.model.aiPanoramArray.count == 0) {
  648. self.curentLabel.hidden = YES;
  649. [alertController dismissViewControllerAnimated:YES completion:^{
  650. [self dismissViewControllerAnimated:YES completion:nil];
  651. }];
  652. } else {
  653. self.curentLabel.hidden = NO;
  654. [alertController dismissViewControllerAnimated:YES completion:nil];
  655. }
  656. }];
  657. }
  658. - (UILabel *)countLabel {
  659. if (!_countLabel) {
  660. _countLabel = [[UILabel alloc]init];
  661. _countLabel.font = [UIFont systemFontOfSize:14];
  662. _countLabel.textColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  663. }
  664. return _countLabel;
  665. }
  666. - (UIView *)bottomView {
  667. if (!_bottomView) {
  668. _bottomView = [[UIView alloc]init];
  669. }
  670. return _bottomView;
  671. }
  672. - (UIView *)modeView {
  673. if (!_modeView) {
  674. _modeView = [[UIView alloc]init];
  675. _modeView.userInteractionEnabled = YES;
  676. }
  677. return _modeView;
  678. }
  679. - (UIScrollView *)scrollView {
  680. if (!_scrollView) {
  681. _scrollView = [[UIScrollView alloc]init];
  682. _scrollView.layer.cornerRadius = 8;
  683. _scrollView.layer.masksToBounds = YES;
  684. _scrollView.delegate = self;
  685. // _scrollView.backgroundColor = [UIColor blackColor];
  686. }
  687. return _scrollView;
  688. }
  689. - (UIButton *)backButton {
  690. if (!_backButton) {
  691. _backButton = [[UIButton alloc]init];
  692. [_backButton setImage:[UIImage loadNamed:@"result-return-btn"] forState:UIControlStateNormal];
  693. [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  694. }
  695. return _backButton;
  696. }
  697. - (UILabel *)backLabel {
  698. if (!_backLabel) {
  699. _backLabel = [[UILabel alloc]init];
  700. _backLabel.text = @"返回";
  701. _backLabel.textColor = [UIColor whiteColor];
  702. }
  703. return _backLabel;
  704. }
  705. - (UILabel *)deleteLabel {
  706. if (!_deleteLabel) {
  707. _deleteLabel = [[UILabel alloc]init];
  708. _deleteLabel.text = @"删除";
  709. _deleteLabel.textColor = [UIColor whiteColor];
  710. }
  711. return _deleteLabel;
  712. }
  713. - (UIButton *)deleteButton {
  714. if (!_deleteButton) {
  715. _deleteButton = [[UIButton alloc]init];
  716. [_deleteButton setImage:[UIImage loadNamed:@"result-delete-btn"] forState:UIControlStateNormal];
  717. [_deleteButton addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
  718. }
  719. return _deleteButton;
  720. }
  721. - (UILabel *)curentLabel {
  722. if (!_curentLabel) {
  723. _curentLabel = [[UILabel alloc]init];
  724. _curentLabel.backgroundColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  725. _curentLabel.layer.cornerRadius = 16;
  726. _curentLabel.layer.masksToBounds = YES;
  727. _curentLabel.textAlignment = NSTextAlignmentCenter;
  728. _curentLabel.textColor = [UIColor whiteColor];
  729. }
  730. return _curentLabel;
  731. }
  732. - (UIImageView *)videoImageView {
  733. if (!_videoImageView) {
  734. _videoImageView = [[UIImageView alloc]init];
  735. _videoImageView.image = [UIImage loadNamed:@"icon_video"];
  736. }
  737. return _videoImageView;
  738. }
  739. @end