PCSPreviewViewController.m 31 KB

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