KMAdvertisementShowScroll_iOS.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // KMAdvertisementShowScroll_iOS.swift
  3. // KMAdvertisement_iOS
  4. //
  5. // Created by lizhe on 2022/11/29.
  6. //
  7. import UIKit
  8. open class KMAdvertisementShowScroll_iOS: KMAdvertisementBaseView {
  9. @IBOutlet var contentView: UIView!
  10. // MARK: 初始化
  11. @IBOutlet weak var collectionView: UICollectionView!
  12. lazy var presenter: KMAdvertisementTableViewPresenter! = KMAdvertisementTableViewPresenter()
  13. //内部使用数据
  14. fileprivate var data: [KMAdvertisementModelSection]?
  15. /**
  16. @abstract 外部传入数据
  17. @param inputData 文件路劲
  18. */
  19. open var inputData: KMAdvertisementModel! {
  20. didSet {
  21. self.presenter.initPresenter(view: self, data: inputData)
  22. }
  23. }
  24. override public var loadCompletion: KMAdvertisementLoadCompletion? {
  25. didSet {
  26. if self.data != nil {
  27. if self.loadCompletion != nil {
  28. self.loadCompletion!(self.data!)
  29. }
  30. }
  31. }
  32. }
  33. convenience init (data: KMAdvertisementModel, superView: UIView) {
  34. self.init(frame: superView.bounds)
  35. self.presenter.initPresenter(view: self, data: data)
  36. superView.addSubview(self)
  37. self.autoresizingMask = [.flexibleHeight , .flexibleWidth]
  38. }
  39. public required init?(coder decoder: NSCoder) {
  40. super.init(coder: decoder)
  41. initContentView()
  42. setup()
  43. }
  44. public override init(frame: CGRect) {
  45. super.init(frame: frame)
  46. initContentView()
  47. setup()
  48. }
  49. private func initContentView() {
  50. //绑定xib
  51. let resource = UINib(nibName: String(describing: self.classForCoder.self),
  52. bundle: Bundle(for: self.classForCoder.self))
  53. resource.instantiate(withOwner: self)
  54. addSubview(contentView)
  55. contentView.translatesAutoresizingMaskIntoConstraints = false
  56. NSLayoutConstraint.activate([
  57. contentView.topAnchor.constraint(equalTo: topAnchor),
  58. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  59. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  60. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  61. contentView.updateConstraintsIfNeeded()
  62. }
  63. func setup() {
  64. //设置代理
  65. let layout = KMAdvertisementFlowLayout()
  66. //设置滚动方向
  67. layout.scrollDirection = .horizontal
  68. // layout.minimumLineSpacing = 10
  69. self.collectionView.collectionViewLayout = layout
  70. self.collectionView.delegate = self
  71. self.collectionView.dataSource = self
  72. self.collectionView.showsHorizontalScrollIndicator = false
  73. self.collectionView.showsVerticalScrollIndicator = false
  74. // self.collectionView.isPagingEnabled = true
  75. self.collectionView.register(UINib(nibName: "KMAdvertisementShowScrollCell_iOS", bundle: nil), forCellWithReuseIdentifier: "KMAdvertisementShowScrollCell_iOS")
  76. self.collectionView.reloadData()
  77. }
  78. public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  79. print("scrollViewDidEndDragging")
  80. }
  81. }
  82. extension KMAdvertisementShowScroll_iOS: KMAdvertisementTableViewPresenterDelegate {
  83. func showData(presenter: KMAdvertisementTableViewPresenter, data: Array<KMAdvertisementModelSection>) {
  84. self.data = data
  85. self.collectionView.reloadData()
  86. if self.loadCompletion != nil {
  87. self.loadCompletion!(self.data!)
  88. }
  89. }
  90. }
  91. extension KMAdvertisementShowScroll_iOS: UICollectionViewDelegate {
  92. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  93. if self.data != nil {
  94. let model = (self.data?[indexPath.section])
  95. let content = model?.content![indexPath.item]
  96. if actionCompletion != nil {
  97. content?.index = indexPath.item
  98. actionCompletion!(.tap, content!)
  99. }
  100. }
  101. }
  102. public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
  103. print("取消选中")
  104. }
  105. }
  106. extension KMAdvertisementShowScroll_iOS: UICollectionViewDataSource {
  107. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  108. if self.data != nil {
  109. let model: KMAdvertisementModelSection = (self.data?[section])!
  110. return model.content!.count
  111. } else {
  112. return 0
  113. }
  114. }
  115. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  116. let cell: KMAdvertisementShowScrollCell_iOS = collectionView.dequeueReusableCell(withReuseIdentifier: "KMAdvertisementShowScrollCell_iOS", for: indexPath) as! KMAdvertisementShowScrollCell_iOS
  117. cell.cancelActionBlock = { [unowned self] in
  118. if self.data != nil {
  119. let model = (self.data?[indexPath.section])
  120. let content = model?.content![indexPath.item]
  121. self.presenter.deleteItem(item: content!)
  122. }
  123. }
  124. cell.buttonActionBlock = { [unowned self] item in
  125. if actionCompletion != nil {
  126. actionCompletion!(.tap, item)
  127. }
  128. }
  129. if self.data != nil {
  130. let model = (self.data?[indexPath.section])
  131. cell.model = model?.content![indexPath.item]
  132. }
  133. return cell
  134. }
  135. }
  136. extension KMAdvertisementShowScroll_iOS: UICollectionViewDelegateFlowLayout {
  137. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  138. return CGSizeMake(self.contentView.bounds.width - 40, self.contentView.bounds.height)
  139. }
  140. }