PCSPreviewViewController.m 30 KB

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