PCSPreviewViewController.m 31 KB

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