PCSPreviewViewController.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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 = AVLayerVideoGravityResizeAspectFill;
  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. UICollectionViewLayoutAttributes*attributes = [self.collectionView layoutAttributesForItemAtIndexPath:self.currentIndexPath];
  222. CGRect rect = attributes.frame;
  223. [self.collectionView setContentOffset:CGPointMake(rect.origin.x, rect.origin.y ) animated:NO];
  224. });
  225. }
  226. //- (void)viewDidLayoutSubviews{
  227. // [self.view layoutIfNeeded];
  228. // [self.collectionView scrollToItemAtIndexPath:self.currentIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
  229. //}
  230. - (UIImageView *)lineImageView {
  231. if (!_lineImageView) {
  232. _lineImageView = [[UIImageView alloc]init];
  233. _lineImageView.image = [UIImage loadNamed:@"icon_preview_line"];
  234. }
  235. return _lineImageView;
  236. }
  237. - (void)updateCountLabelWith:(NSIndexPath *)indexPath {
  238. if (!indexPath) {
  239. return;
  240. }
  241. if (indexPath.section == 0) {
  242. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.continousArray.count];
  243. } else if (indexPath.section == 1) {
  244. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld条", self.currentIndexPath.row + 1, self.model.movieArray.count];
  245. } else if (indexPath.section == 2) {
  246. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.panoramArray.count];
  247. } else {
  248. self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld张", self.currentIndexPath.row + 1, self.model.aiPanoramArray.count];
  249. }
  250. }
  251. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  252. return 4;
  253. }
  254. //返回每个分区的item个数
  255. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  256. if (section == 0) {
  257. return self.model.continousArray.count;
  258. } else if (section == 1) {
  259. return self.model.movieArray.count;
  260. } else if (section == 2) {
  261. return self.model.panoramArray.count;
  262. } else {
  263. return self.model.aiPanoramArray.count;
  264. }
  265. }
  266. //返回每个item
  267. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  268. PCSPreCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PCSPreCollectionCell" forIndexPath:indexPath];
  269. cell.isPlayer = NO;
  270. if (indexPath.section == 0) {
  271. if (self.model.continousArray.count > indexPath.row) {
  272. LenzResourceItemModel *model = self.model.continousArray[indexPath.row];
  273. cell.model = model;
  274. }
  275. } else if (indexPath.section == 1) {
  276. if (self.model.movieArray.count > indexPath.row) {
  277. LenzResourceItemModel *model = self.model.movieArray[indexPath.row];
  278. cell.model = model;
  279. cell.isPlayer = indexPath == self.playerIndexPath;
  280. }
  281. } else if (indexPath.section == 2) {
  282. if (self.model.panoramArray.count > indexPath.row) {
  283. LenzResourceItemModel *model = self.model.panoramArray[indexPath.row];
  284. cell.model = model;
  285. }
  286. } else if (indexPath.section == 3) {
  287. if (self.model.aiPanoramArray.count > indexPath.row) {
  288. LenzResourceItemModel *model = self.model.aiPanoramArray[indexPath.row];
  289. cell.model = model;
  290. }
  291. }
  292. return cell;
  293. }
  294. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  295. if (indexPath.section == 1) {
  296. self.playerIndexPath = indexPath;
  297. [self.collectionView reloadData];
  298. }
  299. }
  300. // 监听UIScrollView的滑动停止
  301. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  302. self.playerIndexPath = nil;
  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. // [self.collectionView reloadData];
  309. }
  310. - (void)updateCurrentIndexPath {
  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)updateViewWhenDelete {
  318. self.playerIndexPath = nil;
  319. NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
  320. NSIndexPath *currentIndexPath = indexPaths.firstObject;
  321. self.currentIndexPath = currentIndexPath;
  322. [self changeLabelStatusWith:currentIndexPath.section + 1];
  323. [self updateCountLabelWith:currentIndexPath];
  324. }
  325. - (void)setupModeView {
  326. [self.modeView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  327. // self.currentModeDataIndex = 0;
  328. CGFloat margin = [UIScreen mainScreen].bounds.size.width == 375 ? 20 : 30;
  329. UIView *lastView = nil;
  330. NSInteger count = 0;
  331. self.currentIndex = 0;
  332. if (self.model.continousArray.count) {
  333. count += self.model.continousArray.count;
  334. self.currentIndex = 1;
  335. UILabel *label = [[UILabel alloc]init];
  336. label.text = @"连拍";
  337. label.tag = 1;
  338. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  339. [label addGestureRecognizer:tap];
  340. label.userInteractionEnabled = YES;
  341. label.textColor = [UIColor whiteColor];
  342. [self.modeView addSubview:label];
  343. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  344. make.left.mas_offset(20);
  345. make.centerY.mas_equalTo(self.countLabel);
  346. }];
  347. lastView = label;
  348. }
  349. if (self.model.movieArray.count) {
  350. count += self.model.movieArray.count;
  351. if (self.currentIndex == 0) {
  352. self.currentIndex = 2;
  353. }
  354. UILabel *label = [[UILabel alloc]init];
  355. label.text = @"视频";
  356. label.tag = 2;
  357. label.textColor = [UIColor whiteColor];
  358. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  359. [label addGestureRecognizer:tap];
  360. label.userInteractionEnabled = YES;
  361. [self.modeView addSubview:label];
  362. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  363. if (lastView) {
  364. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  365. } else {
  366. make.left.mas_equalTo(20);
  367. }
  368. make.centerY.mas_equalTo(self.countLabel);
  369. }];
  370. lastView = label;
  371. }
  372. if (self.model.panoramArray.count) {
  373. count += self.model.panoramArray.count;
  374. if (self.currentIndex == 0) {
  375. self.currentIndex = 3;
  376. }
  377. UILabel *label = [[UILabel alloc]init];
  378. label.text = @"全景";
  379. label.tag = 3;
  380. label.textColor = [UIColor whiteColor];
  381. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  382. [label addGestureRecognizer:tap];
  383. label.userInteractionEnabled = YES;
  384. [self.modeView addSubview:label];
  385. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  386. if (lastView) {
  387. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  388. } else {
  389. make.left.mas_equalTo(20);
  390. }
  391. make.centerY.mas_equalTo(self.countLabel);
  392. }];
  393. lastView = label;
  394. }
  395. if (self.model.aiPanoramArray.count) {
  396. count += self.model.aiPanoramArray.count;
  397. if (self.currentIndex == 0) {
  398. self.currentIndex = 4;
  399. }
  400. UILabel *label = [[UILabel alloc]init];
  401. label.text = @"智能全景";
  402. label.tag = 4;
  403. label.textColor = [UIColor whiteColor];
  404. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeAction:)];
  405. [label addGestureRecognizer:tap];
  406. label.userInteractionEnabled = YES;
  407. [self.modeView addSubview:label];
  408. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  409. if (lastView) {
  410. make.left.mas_equalTo(lastView.mas_right).mas_offset(margin);
  411. } else {
  412. make.left.mas_equalTo(20);
  413. }
  414. make.centerY.mas_equalTo(self.countLabel);
  415. }];
  416. lastView = label;
  417. }
  418. self.countLabel.text = [NSString stringWithFormat:@"共%ld笔数据", count];
  419. [self.modeView addSubview:self.lineImageView];
  420. }
  421. - (void)changeAction:(UITapGestureRecognizer *)tap {
  422. [self.view layoutIfNeeded];
  423. UILabel *label = (UILabel *)tap.view;
  424. self.currentIndexPath = [NSIndexPath indexPathForItem:0 inSection:label.tag - 1];
  425. // [self.collectionView scrollToItemAtIndexPath:self.currentIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
  426. UICollectionViewLayoutAttributes*attributes = [self.collectionView layoutAttributesForItemAtIndexPath:self.currentIndexPath];
  427. CGRect rect = attributes.frame;
  428. [self.collectionView setContentOffset:CGPointMake(rect.origin.x, rect.origin.y ) animated:NO];
  429. [self changeLabelStatusWith:label.tag];
  430. [self updateCountLabelWith:self.currentIndexPath];
  431. // [self updateScrollWith:label.tag];
  432. }
  433. - (void)changeLabelStatusWith:(NSInteger)index {
  434. if(index <=0) {
  435. return;
  436. }
  437. UILabel *label1 = [self.modeView viewWithTag:1];
  438. if (label1) {
  439. label1.textColor = [UIColor whiteColor];
  440. }
  441. UILabel *label2 = [self.modeView viewWithTag:2];
  442. if (label2) {
  443. label2.textColor = [UIColor whiteColor];
  444. }
  445. UILabel *label3 = [self.modeView viewWithTag:3];
  446. if (label3) {
  447. label3.textColor = [UIColor whiteColor];
  448. }
  449. UILabel *label4 = [self.modeView viewWithTag:4];
  450. if (label4) {
  451. label4.textColor = [UIColor whiteColor];
  452. }
  453. UILabel *label = (UILabel *)[self.modeView viewWithTag:index];
  454. if (label) {
  455. label.textColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  456. [self.lineImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  457. make.top.mas_equalTo(label.mas_bottom);
  458. make.centerX.mas_equalTo(label);
  459. make.width.mas_offset(18);
  460. make.height.mas_offset(9);
  461. }];
  462. }
  463. self.currentIndex = index;
  464. }
  465. - (void)updateScrollWith:(NSInteger)tag {
  466. if (tag > 0) {
  467. NSArray <LenzResourceItemModel *> *source = nil;
  468. if (tag == 1) {
  469. source = self.model.continousArray;
  470. } else if (tag == 2) {
  471. source = self.model.movieArray;
  472. } else if (tag == 3) {
  473. source = self.model.panoramArray;
  474. } else if (tag == 4) {
  475. source = self.model.aiPanoramArray;
  476. }
  477. [self updateScrollViewWith:source];
  478. }
  479. }
  480. - (void)updateScrollViewWith:(NSArray <LenzResourceItemModel *> *)array {
  481. CGFloat width = CGRectGetWidth(self.scrollView.frame);
  482. CGFloat height = CGRectGetHeight(self.scrollView.frame);
  483. // self.currentModeDataIndex = 0;
  484. [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  485. self.scrollView.pagingEnabled = YES;
  486. self.scrollView.contentSize = CGSizeMake(width * array.count, height);
  487. __block UIImageView *lastImageView = nil;
  488. [array enumerateObjectsUsingBlock:^(LenzResourceItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  489. UIImageView *imageView = [[UIImageView alloc]init];
  490. imageView.image = obj.image;
  491. imageView.tag = idx;
  492. // imageView.contentMode = UIViewContentModeScaleAspectFill;
  493. // imageView.clipsToBounds = YES;
  494. [self.scrollView addSubview:imageView];
  495. [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  496. make.top.mas_offset(0);
  497. make.height.mas_offset(height);
  498. make.width.mas_offset(width);
  499. if (lastImageView) {
  500. make.left.mas_equalTo(lastImageView.mas_right);
  501. } else {
  502. make.left.mas_offset(0);
  503. }
  504. }];
  505. UIImageView *videoImageView = [[UIImageView alloc]init];
  506. videoImageView.image = [UIImage loadNamed:@"icon_video"];
  507. [imageView addSubview:videoImageView];
  508. [videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  509. make.center.mas_equalTo(imageView);
  510. make.width.height.mas_offset(48);
  511. }];
  512. [self.view setNeedsLayout];
  513. [self.view layoutIfNeeded];
  514. if (obj.mode == SDK_CAPTURE_MODE_MOVIE) {
  515. videoImageView.hidden = NO;
  516. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(videoAction:)];
  517. [imageView addGestureRecognizer:tap];
  518. imageView.userInteractionEnabled = YES;
  519. AVPlayerViewController *player = [[AVPlayerViewController alloc]init];
  520. player.view.frame = CGRectMake(0, 0, CGRectGetWidth(self.scrollView.frame), CGRectGetHeight(self.scrollView.frame));
  521. player.view.hidden = YES;
  522. [imageView addSubview:player.view];
  523. [self.allPlayer addObject:player];
  524. } else {
  525. videoImageView.hidden = YES;
  526. }
  527. lastImageView = imageView;
  528. }];
  529. // [self.player.view bringSubviewToFront:self.scrollView];
  530. self.curentLabel.text = [NSString stringWithFormat:@"第%d/%ld条", 1, array.count];
  531. }
  532. - (void)videoAction:(UITapGestureRecognizer *)tap {
  533. UIImageView *view = (UIImageView *)tap.view;
  534. if (self.model.movieArray.count > view.tag) {
  535. LenzResourceItemModel *model = self.model.movieArray[view.tag];
  536. AVPlayer *player = [AVPlayer playerWithURL:[NSURL URLWithString:model.path]];
  537. if (self.lastPlayer) {
  538. [self.lastPlayer.player pause];
  539. self.lastPlayer.player = nil;
  540. self.lastPlayer.view.hidden = YES;
  541. }
  542. AVPlayerViewController *playerVC = self.allPlayer[view.tag];
  543. playerVC.view.hidden = NO;
  544. playerVC.player = player;
  545. [playerVC.player play];
  546. self.lastPlayer = playerVC;
  547. }
  548. }
  549. - (void)backAction {
  550. [self dismissViewControllerAnimated:YES completion:nil];
  551. }
  552. - (void)deleteAction {
  553. NSString *title = @"图片删除后无法恢复,请确认!";
  554. if (self.currentIndex == 2) {
  555. title = @"视频删除后无法恢复,请确认!";
  556. }
  557. [QuitMultipleModeAlertViewController show:self title:@"确认提醒" text:title leftBtnTitle:@"取消" rightBtnTitle:@"确定" withLeftButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  558. [alertController dismissViewControllerAnimated:YES completion:nil];
  559. } rightButtonCallBack:^(QuitMultipleModeAlertViewController * _Nonnull alertController) {
  560. if (self.currentIndex == 1) {
  561. if (self.model.continousArray.count > self.currentIndexPath.item) {
  562. [self.model.continousArray removeObjectAtIndex:self.currentIndexPath.item];
  563. }
  564. } else if (self.currentIndex == 2) {
  565. if (self.model.movieArray.count > self.currentIndexPath.item) {
  566. [self.model.movieArray removeObjectAtIndex:self.currentIndexPath.item];
  567. }
  568. } else if (self.currentIndex == 3) {
  569. if (self.model.panoramArray.count > self.currentIndexPath.item) {
  570. [self.model.panoramArray removeObjectAtIndex:self.currentIndexPath.item];
  571. }
  572. } else if (self.currentIndex == 4) {
  573. if (self.model.aiPanoramArray.count > self.currentIndexPath.item) {
  574. [self.model.aiPanoramArray removeObjectAtIndex:self.currentIndexPath.item];
  575. }
  576. }
  577. [self setupModeView];
  578. [self scrollViewDidEndDecelerating:self.collectionView];
  579. // [self updateScrollWith:self.currentIndex];
  580. [self.collectionView reloadData];
  581. [self.view setNeedsLayout];
  582. [self.view layoutIfNeeded];
  583. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  584. [self updateCurrentIndexPath];
  585. });
  586. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  587. [self updateViewWhenDelete];
  588. // });
  589. if (self.dataChangeBlock) {
  590. self.dataChangeBlock();
  591. }
  592. if (self.model.continousArray.count == 0 &&
  593. self.model.movieArray.count == 0 &&
  594. self.model.panoramArray.count == 0 &&
  595. self.model.aiPanoramArray.count == 0) {
  596. self.curentLabel.hidden = YES;
  597. // [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  598. [alertController dismissViewControllerAnimated:YES completion:^{
  599. [self dismissViewControllerAnimated:YES completion:nil];
  600. }];
  601. } else {
  602. self.curentLabel.hidden = NO;
  603. [alertController dismissViewControllerAnimated:YES completion:nil];
  604. }
  605. }];
  606. }
  607. //- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  608. // CGFloat x = scrollView.contentOffset.x;
  609. // CGFloat width = CGRectGetWidth(self.scrollView.frame);
  610. // NSInteger count = self.scrollView.contentSize.width/width;
  611. // NSInteger index = x/width;
  612. //// if (x - index * width > 0) {
  613. //// //下一组
  614. //// if (self.currentIndex == 1) {
  615. //// if (self.model.movieArray.count > 0) {
  616. //// [self changeLabelStatusWith:2];
  617. //// [self updateScrollWith:2];
  618. //// self.currentIndex = 2;
  619. //// } else if (self.model.panoramArray.count > 0) {
  620. //// [self changeLabelStatusWith:3];
  621. //// [self updateScrollWith:3];
  622. //// self.currentIndex = 3;
  623. //// }
  624. //// }
  625. //// } else {
  626. ////
  627. //// }
  628. // self.currentModeDataIndex = index;
  629. // self.curentLabel.text = [NSString stringWithFormat:@"第%ld/%ld条",index + 1, count];
  630. //
  631. //}
  632. - (UILabel *)countLabel {
  633. if (!_countLabel) {
  634. _countLabel = [[UILabel alloc]init];
  635. _countLabel.font = [UIFont systemFontOfSize:14];
  636. _countLabel.textColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  637. }
  638. return _countLabel;
  639. }
  640. - (UIView *)bottomView {
  641. if (!_bottomView) {
  642. _bottomView = [[UIView alloc]init];
  643. }
  644. return _bottomView;
  645. }
  646. - (UIView *)modeView {
  647. if (!_modeView) {
  648. _modeView = [[UIView alloc]init];
  649. _modeView.userInteractionEnabled = YES;
  650. }
  651. return _modeView;
  652. }
  653. - (UIScrollView *)scrollView {
  654. if (!_scrollView) {
  655. _scrollView = [[UIScrollView alloc]init];
  656. _scrollView.layer.cornerRadius = 8;
  657. _scrollView.layer.masksToBounds = YES;
  658. _scrollView.delegate = self;
  659. _scrollView.backgroundColor = [UIColor blackColor];
  660. }
  661. return _scrollView;
  662. }
  663. - (UIButton *)backButton {
  664. if (!_backButton) {
  665. _backButton = [[UIButton alloc]init];
  666. [_backButton setImage:[UIImage loadNamed:@"result-return-btn"] forState:UIControlStateNormal];
  667. [_backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
  668. }
  669. return _backButton;
  670. }
  671. - (UILabel *)backLabel {
  672. if (!_backLabel) {
  673. _backLabel = [[UILabel alloc]init];
  674. _backLabel.text = @"返回";
  675. _backLabel.textColor = [UIColor whiteColor];
  676. }
  677. return _backLabel;
  678. }
  679. - (UILabel *)deleteLabel {
  680. if (!_deleteLabel) {
  681. _deleteLabel = [[UILabel alloc]init];
  682. _deleteLabel.text = @"删除";
  683. _deleteLabel.textColor = [UIColor whiteColor];
  684. }
  685. return _deleteLabel;
  686. }
  687. - (UIButton *)deleteButton {
  688. if (!_deleteButton) {
  689. _deleteButton = [[UIButton alloc]init];
  690. [_deleteButton setImage:[UIImage loadNamed:@"result-delete-btn"] forState:UIControlStateNormal];
  691. [_deleteButton addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
  692. }
  693. return _deleteButton;
  694. }
  695. - (UILabel *)curentLabel {
  696. if (!_curentLabel) {
  697. _curentLabel = [[UILabel alloc]init];
  698. _curentLabel.backgroundColor = [UIColor colorWithRed:231/255.0 green:108/255.0 blue:30/255.0 alpha:1];
  699. _curentLabel.layer.cornerRadius = 16;
  700. _curentLabel.layer.masksToBounds = YES;
  701. _curentLabel.textAlignment = NSTextAlignmentCenter;
  702. _curentLabel.textColor = [UIColor whiteColor];
  703. }
  704. return _curentLabel;
  705. }
  706. - (UIImageView *)videoImageView {
  707. if (!_videoImageView) {
  708. _videoImageView = [[UIImageView alloc]init];
  709. _videoImageView.image = [UIImage loadNamed:@"icon_video"];
  710. }
  711. return _videoImageView;
  712. }
  713. @end