// // TSAIPhotoBrowseVC.swift // AIEmoji // // Created by 100Years on 2025/4/2. // private let cellId = "TSAIPhotoBrowseCell" private let videoCellId = "TSAIVideoBrowseCell" class TSAIPhotoBrowseVC: TSBaseVC { var dataModelArray = [TSActionInfoModel]() var currentImage:UIImage?{ let cell = collectionView.cellForItem(at: IndexPath(item: currentIndex, section: 0)) as? TSAIPhotoBrowseCell var image = cell?.netWorkImageView.image image = image?.pngImage return image } var currentModel:TSActionInfoModel?{ if let model = dataModelArray.safeObj(At: currentIndex){ return model } return nil } var currentIndex:Int = 0 { didSet{ reloadUI() } } lazy var collectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() layout.scrollDirection = .vertical let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) collectionView.delegate = self collectionView.dataSource = self collectionView.showsVerticalScrollIndicator = false collectionView.showsHorizontalScrollIndicator = false collectionView.backgroundColor = .clear if #available(iOS 11.0, *) { collectionView.contentInsetAdjustmentBehavior = .never } collectionView.register(TSAIPhotoBrowseCell.self, forCellWithReuseIdentifier: cellId) collectionView.register(TSAIVideoBrowseCell.self, forCellWithReuseIdentifier: videoCellId) collectionView.isPagingEnabled = true collectionView.isHidden = true if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.minimumInteritemSpacing = 0 flowLayout.minimumLineSpacing = 0 flowLayout.scrollDirection = .horizontal flowLayout.itemSize = CGSize(width: k_ScreenWidth, height: k_ScreenHeight) } return collectionView }() //保存按钮 lazy var bigSaveBtn: UIButton = { let bigSaveBtn = kCreateNormalSubmitBtn(title: "Save".localized) { [weak self] in guard let self = self else { return } clickSubmitBtn() } bigSaveBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) bigSaveBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) bigSaveBtn.frame = CGRectMake(0, 0, 252, 44) bigSaveBtn.cornerRadius = 22.0 return bigSaveBtn }() lazy var xBtn: UIButton = { let xBtn = UIButton.createButton(image: UIImage(named: "close_gray")) { [weak self] in guard let self = self else { return } clickXBtn() } return xBtn }() override func createView() { super.createView() setNavBarViewHidden(true) contentView.addSubview(collectionView) collectionView.snp.makeConstraints { make in make.edges.equalTo(0) } DispatchQueue.main.async { self.collectionView.isHidden = false self.collectionView.reloadData() self.collectionView.setContentOffset(CGPoint(x: CGFloat(self.currentIndex) * self.collectionView.frame.size.width, y: 0), animated: false) if let flowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout { flowLayout.itemSize = self.collectionView.bounds.size } } contentView.addSubview(bigSaveBtn) bigSaveBtn.snp.makeConstraints { make in make.centerX.equalToSuperview() make.bottom.equalTo(-8-k_Height_safeAreaInsetsBottom()) make.width.equalTo(bigSaveBtn.width) make.height.equalTo(bigSaveBtn.height) } //关闭按钮 contentView.addSubview(xBtn) xBtn.snp.makeConstraints { make in make.top.equalTo(k_Height_StatusBar + 4) make.leading.equalTo(16) make.width.equalTo(36) make.height.equalTo(36) } } override func dealThings() { // 添加下拉手势 // let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleDismissPan(_:))) // view.addGestureRecognizer(panGesture) addPullDownClosePage() } @objc func clickSubmitBtn(){ if JudgeVip(){ return } guard let currentModel = currentModel else { return } let urlString = currentModel.response.resultUrl if currentModel.isVideo { TSDownloadManager.getDownLoadVideo(urlString: urlString) { url, _ in if let url = url { PhotoManagerShared.saveVideoToAlbum(videoURL: url) { success, error in if success { kSaveSuccesswShared.show(atView: self.view) }else{ debugPrint(error) } } } } }else{ UIImageView.downloadImageWithProgress(urlString: urlString) { image in if let image = image{ PhotoManagerShared.saveImageToAlbum(image) { success, error in if success { kSaveSuccesswShared.show(atView: self.view) }else{ debugPrint(error) } } } } } } func JudgeVip() -> Bool { return kJudgeVip(externalBool: currentModel?.response.vip ?? false , vc: self, closePageBlock: nil) } @objc func clickXBtn(){ pop() } } extension TSAIPhotoBrowseVC { // 手势处理方法 // @objc func handleDismissPan(_ gesture: UIPanGestureRecognizer) { // let translation = gesture.translation(in: view) // let progress = translation.y / view.bounds.height // // switch gesture.state { // case .changed: // view.transform = CGAffineTransform(translationX: 0, y: max(0, translation.y)) // case .ended: // if progress > 0.5 || gesture.velocity(in: view).y > 1000 { // dismiss(animated: true) // } else { // UIView.animate(withDuration: 0.3) { // self.view.transform = .identity // } // } // default: // break // } // } func reloadUI() { } } //MARK: UICollectionViewDataSource extension TSAIPhotoBrowseVC:UICollectionViewDataSource,UICollectionViewDelegate { func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { resetIndexWithOffset(scrollView) } func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { resetIndexWithOffset(scrollView) } private func resetIndexWithOffset(_ scrollView: UIScrollView) { let item = Int((scrollView.contentOffset.x / scrollView.bounds.width).rounded()) currentIndex = item } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return dataModelArray.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if let model = dataModelArray.safeObj(At: indexPath.item){ if model.isVideo { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: videoCellId, for: indexPath) as! TSAIVideoBrowseCell cell.model = model return cell } } let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! TSAIPhotoBrowseCell if let model = dataModelArray.safeObj(At: indexPath.item){ cell.model = model } return cell } func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { if let model = dataModelArray.safeObj(At: indexPath.item){ if model.isVideo { guard let cell = cell as? TSAIVideoBrowseCell else { return } cell.model = model }else{ guard let cell = cell as? TSAIPhotoBrowseCell else { return } cell.model = model } } } func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { if let model = dataModelArray.safeObj(At: indexPath.item){ if model.isVideo { guard let cell = cell as? TSAIVideoBrowseCell else { return } cell.videoPlayerVC?.playPause() } } } } class TSAIPhotoBrowseCell : TSBaseCollectionCell{ lazy var exampleView: UIView = { let exampleView = UIView() exampleView.backgroundColor = "#232323".uiColor.withAlphaComponent(0.3) let textLabel = UILabel.createLabel( text: "Example".localized, font: .font(size: 12), textColor: .white ) exampleView.addSubview(textLabel) textLabel.snp.makeConstraints { make in make.top.edges.equalTo(UIEdgeInsets(top: 4, left: 6, bottom: 4, right: 6)) } exampleView.isHidden = true exampleView.cornerRadius = 10.0 return exampleView }() lazy var netWorkImageView : UIImageView = { let netWorkImageView = UIImageView.createImageView(imageName: "",contentMode: .scaleAspectFit,corner: 24.0) return netWorkImageView }() lazy var vipImageView: UIImageView = { let vipImageView = UIImageView.createImageView(imageName:"vip_side_icon") vipImageView.contentMode = .scaleToFill vipImageView.isHidden = true return vipImageView }() override func creatUI() { let w = k_ScreenWidth let h = k_ScreenHeight bgContentView.addSubview(netWorkImageView) netWorkImageView.snp.makeConstraints { make in make.center.equalToSuperview() make.width.equalTo(w) make.height.equalTo(h) } netWorkImageView.addSubview(vipImageView) vipImageView.snp.makeConstraints { make in make.width.height.equalTo(40) make.top.equalTo(-5) make.trailing.equalTo(5) } } var model:TSActionInfoModel = TSActionInfoModel(){ didSet{ dePrint("TSAIPhotoBrowseCell didSet model") if model.modelType == .example { netWorkImageView.image = UIImage(named:model.response.resultUrl) netWorkImageView.adaptiveScale() }else{ netWorkImageView.setAsyncImage(urlString: model.response.resultUrl,placeholder: kPlaceholderImage,contentMode: .scaleAspectFit, completion: { [weak self] image in guard let self = self else { return } netWorkImageView.adaptiveScale() }) } vipImageView.isHidden = !model.response.vip } } } class TSAIVideoBrowseCell : TSBaseCollectionCell{ var videoPlayerVC: TSAIListVideoPlayerVC? override func creatUI() { } var model:TSActionInfoModel = TSActionInfoModel(){ didSet{ self.videoPlayerVC?.view.removeFromSuperview() self.videoPlayerVC = TSAIListVideoPlayerVC(videoURL: self.model.videoURL) self.bgContentView.addSubview(self.videoPlayerVC!.view) self.videoPlayerVC!.view.snp.remakeConstraints { make in make.center.equalToSuperview() make.width.equalTo(k_ScreenWidth) make.height.equalTo(k_ScreenHeight) } } } }