PCSPreviewViewController.m 30 KB

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