PQSelectedMaterialListView.swift 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // PQSelectedMaterialListView.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2021/5/16.
  6. // Copyright © 2021 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. class PQSelectedMaterialListView: UIView {
  10. var photoData: [PQEditVisionTrackMaterialsModel] = Array<PQEditVisionTrackMaterialsModel>.init() // 相册数据
  11. var deletedMaterialHandle: ((_ materialData: PQEditVisionTrackMaterialsModel?, _ isDissmiss: Bool) -> Void)? // 删除已选素材回调
  12. var detailMaterialHandle: ((_ indexPath: IndexPath, _ currentMaterialData: PQEditVisionTrackMaterialsModel?) -> Void)? // 点击详情
  13. lazy var photoCollectionView: UICollectionView = {
  14. let photoFlowLayout = UICollectionViewFlowLayout()
  15. photoFlowLayout.itemSize = CGSize(width: 74, height: 74)
  16. photoFlowLayout.minimumLineSpacing = cDefaultMargin
  17. photoFlowLayout.minimumInteritemSpacing = 0
  18. photoFlowLayout.scrollDirection = .horizontal
  19. let photoCollectionView = UICollectionView(frame: bounds, collectionViewLayout: photoFlowLayout)
  20. photoCollectionView.register(PQChoseMaterialCell.self, forCellWithReuseIdentifier: String(describing: PQChoseMaterialCell.self))
  21. photoCollectionView.showsVerticalScrollIndicator = false
  22. photoCollectionView.showsHorizontalScrollIndicator = false
  23. photoCollectionView.delegate = self
  24. photoCollectionView.dataSource = self
  25. photoCollectionView.backgroundColor = UIColor.clear
  26. if #available(iOS 11.0, *) {
  27. photoCollectionView.contentInsetAdjustmentBehavior = .never
  28. } else {}
  29. return photoCollectionView
  30. }()
  31. override init(frame: CGRect) {
  32. super.init(frame: frame)
  33. backgroundColor = PQBFConfig.shared.styleBackGroundColor
  34. addSubview(photoCollectionView)
  35. }
  36. required init?(coder _: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. /// 添加新素材
  40. /// - Parameter itemData: <#itemData description#>
  41. /// - Returns: <#description#>
  42. func addMaterialData(materialData: PQEditVisionTrackMaterialsModel) {
  43. let temp = photoData.firstIndex { item in
  44. item.asset == materialData.asset
  45. }
  46. if temp == nil, materialData.isSelected {
  47. photoData.append(materialData)
  48. photoCollectionView.reloadData()
  49. photoCollectionView.scrollToItem(at: IndexPath(item: photoData.count - 1, section: 0), at: .right, animated: true)
  50. } else if temp != nil, !materialData.isSelected {
  51. photoData.remove(at: temp ?? 0)
  52. photoCollectionView.reloadData()
  53. }
  54. }
  55. }
  56. extension PQSelectedMaterialListView: UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {
  57. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  58. return photoData.count
  59. }
  60. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  61. let cell = PQChoseMaterialCell.choseMaterialCell(collectionView: collectionView, indexPath: indexPath)
  62. cell.isShowMediaTag = false
  63. cell.isAdded = true
  64. cell.materialData = photoData[indexPath.item]
  65. cell.materialClicHandle = { [weak self] _, _ in
  66. if self?.deletedMaterialHandle != nil {
  67. self?.deletedMaterialHandle!(self?.photoData[indexPath.item], (self?.photoData.count ?? 0) <= 1)
  68. }
  69. self?.photoData[indexPath.item].isSelected = false
  70. self?.photoData.remove(at: indexPath.item)
  71. collectionView.reloadData()
  72. }
  73. return cell
  74. }
  75. func collectionView(_: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  76. if detailMaterialHandle != nil {
  77. detailMaterialHandle!(indexPath, photoData[indexPath.item])
  78. }
  79. }
  80. }