PCSPreviewViewController.m 33 KB

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