KMAdvertisementShowScroll_iOS.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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: [KMAdvertisementModel.Section]?
  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. convenience init (data: KMAdvertisementModel, superView: UIView) {
  25. self.init(frame: superView.bounds)
  26. self.presenter.initPresenter(view: self, data: data)
  27. superView.addSubview(self)
  28. self.autoresizingMask = [.flexibleHeight , .flexibleWidth]
  29. }
  30. public required init?(coder decoder: NSCoder) {
  31. super.init(coder: decoder)
  32. initContentView()
  33. setup()
  34. }
  35. public override init(frame: CGRect) {
  36. super.init(frame: frame)
  37. initContentView()
  38. setup()
  39. }
  40. private func initContentView() {
  41. //绑定xib
  42. let resource = UINib(nibName: String(describing: self.classForCoder.self),
  43. bundle: Bundle(for: self.classForCoder.self))
  44. resource.instantiate(withOwner: self)
  45. addSubview(contentView)
  46. contentView.translatesAutoresizingMaskIntoConstraints = false
  47. NSLayoutConstraint.activate([
  48. contentView.topAnchor.constraint(equalTo: topAnchor),
  49. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  50. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  51. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  52. contentView.updateConstraintsIfNeeded()
  53. }
  54. func setup() {
  55. //设置代理
  56. let layout = KMAdvertisementFlowLayout()
  57. //设置滚动方向
  58. layout.scrollDirection = .horizontal
  59. // layout.minimumLineSpacing = 10
  60. self.collectionView.collectionViewLayout = layout
  61. self.collectionView.delegate = self
  62. self.collectionView.dataSource = self
  63. self.collectionView.showsHorizontalScrollIndicator = false
  64. self.collectionView.showsVerticalScrollIndicator = false
  65. // self.collectionView.isPagingEnabled = true
  66. self.collectionView.register(UINib(nibName: "KMAdvertisementShowScrollCell_iOS", bundle: nil), forCellWithReuseIdentifier: "KMAdvertisementShowScrollCell_iOS")
  67. self.collectionView.reloadData()
  68. }
  69. public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
  70. print("scrollViewDidEndDragging")
  71. }
  72. }
  73. extension KMAdvertisementShowScroll_iOS: KMAdvertisementTableViewPresenterDelegate {
  74. func showData(presenter: KMAdvertisementTableViewPresenter, data: Array<KMAdvertisementModel.Section>) {
  75. self.data = data
  76. self.collectionView.reloadData()
  77. }
  78. }
  79. extension KMAdvertisementShowScroll_iOS: UICollectionViewDelegate {
  80. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  81. if self.data != nil {
  82. let model = (self.data?[indexPath.section])
  83. var content = model?.content[indexPath.item]
  84. if actionCompletion != nil {
  85. content?.index = indexPath.item
  86. actionCompletion!(content!)
  87. }
  88. }
  89. }
  90. public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
  91. print("取消选中")
  92. }
  93. }
  94. extension KMAdvertisementShowScroll_iOS: UICollectionViewDataSource {
  95. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  96. if self.data != nil {
  97. let model: KMAdvertisementModel.Section = (self.data?[section])!
  98. return model.content.count
  99. } else {
  100. return 0
  101. }
  102. }
  103. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  104. let cell: KMAdvertisementShowScrollCell_iOS = collectionView.dequeueReusableCell(withReuseIdentifier: "KMAdvertisementShowScrollCell_iOS", for: indexPath) as! KMAdvertisementShowScrollCell_iOS
  105. if self.data != nil {
  106. let model = (self.data?[indexPath.section])
  107. cell.model = model?.content[indexPath.item]
  108. }
  109. return cell
  110. }
  111. }
  112. extension KMAdvertisementShowScroll_iOS: UICollectionViewDelegateFlowLayout {
  113. public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  114. return CGSizeMake(self.contentView.bounds.width - 40, self.contentView.bounds.height)
  115. }
  116. }