TSBigIconBrowseVC.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // TSBigIconBrowseVC.swift
  3. // AIEmoji
  4. //
  5. // Created by 100Years on 2025/2/20.
  6. //
  7. private let cellId = "TSBigIconBrowseCell"
  8. class TSBigIconBrowseVC: TSBottomAlertVC {
  9. var dataModelArray = [TSActionInfoModel]()
  10. var currentImage:UIImage?{
  11. let cell = collectionView.cellForItem(at: IndexPath(item: currentIndex, section: 0)) as? TSBigIconBrowseCell
  12. var image = cell?.netWorkImageView.image
  13. image = image?.pngImage
  14. return image
  15. }
  16. var currentModel:TSActionInfoModel?{
  17. if let model = dataModelArray.safeObj(At: currentIndex){
  18. return model
  19. }
  20. return nil
  21. }
  22. var currentIndex:Int = 0 {
  23. didSet{
  24. reloadUI()
  25. }
  26. }
  27. lazy var collectionView: UICollectionView = {
  28. let layout = UICollectionViewFlowLayout()
  29. layout.scrollDirection = .vertical
  30. let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
  31. collectionView.delegate = self
  32. collectionView.dataSource = self
  33. collectionView.showsVerticalScrollIndicator = false
  34. collectionView.showsHorizontalScrollIndicator = false
  35. collectionView.backgroundColor = .clear
  36. if #available(iOS 11.0, *) {
  37. collectionView.contentInsetAdjustmentBehavior = .never
  38. }
  39. collectionView.register(TSBigIconBrowseCell.self, forCellWithReuseIdentifier: cellId)
  40. collectionView.isPagingEnabled = true
  41. collectionView.isHidden = true
  42. if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
  43. flowLayout.minimumInteritemSpacing = 0
  44. flowLayout.minimumLineSpacing = 0
  45. flowLayout.scrollDirection = .horizontal
  46. flowLayout.itemSize = CGSize(width: k_ScreenWidth, height: 490*kDesignScale)
  47. }
  48. return collectionView
  49. }()
  50. lazy var leftBtn: UIButton = {
  51. let leftBtn = UIButton.createButton(image: UIImage(named: "page_left")){ [weak self] in
  52. guard let self = self else { return }
  53. self.collectionView.scrollToItem(at: IndexPath(item: self.currentIndex-1, section: 0), at: .left, animated: true)
  54. }
  55. return leftBtn
  56. }()
  57. lazy var rightBtn: UIButton = {
  58. let rightBtn = UIButton.createButton(image: UIImage(named: "page_right")){ [weak self] in
  59. guard let self = self else { return }
  60. self.collectionView.scrollToItem(at: IndexPath(item: self.currentIndex+1, section: 0), at: .left, animated: true)
  61. }
  62. return rightBtn
  63. }()
  64. override func createView() {
  65. super.createView()
  66. submitBtn.isHidden = true
  67. bottomView.frame = CGRectMake(0, 92, k_ScreenWidth, k_ScreenHeight-92)
  68. cancelBtn.setTitle("Copy Text".localized, for: .normal)
  69. bottomView.addSubview(collectionView)
  70. collectionView.snp.makeConstraints { make in
  71. make.leading.trailing.equalTo(0)
  72. make.top.equalTo(36)
  73. make.bottom.equalTo(confirmBtn.snp.top).offset(-30)
  74. }
  75. bottomView.addSubview(leftBtn)
  76. leftBtn.snp.makeConstraints { make in
  77. make.leading.equalTo(17)
  78. make.top.equalTo(244)
  79. make.width.height.equalTo(40)
  80. }
  81. bottomView.addSubview(rightBtn)
  82. rightBtn.snp.makeConstraints { make in
  83. make.trailing.equalTo(-18)
  84. make.top.equalTo(244)
  85. make.width.height.equalTo(40)
  86. }
  87. kDelayMainShort {
  88. self.collectionView.isHidden = false
  89. self.collectionView.reloadData()
  90. self.collectionView.setContentOffset(CGPoint(x: CGFloat(self.currentIndex) * self.collectionView.frame.size.width, y: 0), animated: false)
  91. if let flowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
  92. flowLayout.itemSize = self.collectionView.bounds.size
  93. }
  94. }
  95. }
  96. @objc override func clickCancelBtn(){
  97. if JudgeVip(){
  98. return
  99. }
  100. if let model = currentModel{
  101. //拷贝文字到截切板
  102. UIPasteboard.general.string = model.request.prompt
  103. kSaveSuccesswShared.show(atView: self.view,text: "Copyed Successfully".localized,showViewBtn:false)
  104. }else{
  105. kShowToastDataMissing()
  106. }
  107. }
  108. @objc override func clickConfirmBtn(){
  109. if JudgeVip(){
  110. return
  111. }
  112. if let image = currentImage{
  113. PhotoManagerShared.saveImageToAlbum(image) { success, error in
  114. if success {
  115. kSaveSuccesswShared.show(atView: self.view)
  116. }else{
  117. debugPrint(error)
  118. }
  119. }
  120. }else{
  121. kShowToastDataMissing()
  122. }
  123. }
  124. func JudgeVip() -> Bool {
  125. return kJudgeVip(externalBool: currentModel?.response.vip ?? false , vc: self, closePageBlock: nil)
  126. }
  127. }
  128. extension TSBigIconBrowseVC {
  129. func reloadUI() {
  130. //判断前后是否还有,以此来确定左右翻页按钮
  131. var isHiddenLeft = false
  132. var isHiddenRight = false
  133. if dataModelArray.count == 1{
  134. isHiddenLeft = true
  135. isHiddenRight = true
  136. }else if currentIndex == 0 {
  137. isHiddenLeft = true
  138. }else if currentIndex+1 >= dataModelArray.count {
  139. isHiddenRight = true
  140. }
  141. leftBtn.isHidden = isHiddenLeft
  142. rightBtn.isHidden = isHiddenRight
  143. }
  144. }
  145. //MARK: UICollectionViewDataSource
  146. extension TSBigIconBrowseVC:UICollectionViewDataSource,UICollectionViewDelegate {
  147. func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  148. resetIndexWithOffset(scrollView)
  149. }
  150. func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
  151. resetIndexWithOffset(scrollView)
  152. }
  153. private func resetIndexWithOffset(_ scrollView: UIScrollView) {
  154. let item = Int((scrollView.contentOffset.x / scrollView.bounds.width).rounded())
  155. currentIndex = item
  156. }
  157. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  158. return dataModelArray.count
  159. }
  160. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  161. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! TSBigIconBrowseCell
  162. if let model = dataModelArray.safeObj(At: indexPath.item){
  163. cell.netWorkImageView.setAsyncImage(urlString: model.response.resultUrl,placeholder: kPlaceholderImage,contentMode: .scaleAspectFill)
  164. cell.vipImageView.isHidden = !model.response.vip
  165. if model.request.promptSort.count > 0 {
  166. cell.textLabel.text = model.request.promptSort
  167. }else{
  168. cell.textLabel.text = model.request.prompt
  169. }
  170. }
  171. return cell
  172. }
  173. }