KMAdvertisementShowView_iOS.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // KMAdvertisementShowView_iOS.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/11/29.
  6. //
  7. import UIKit
  8. import Kingfisher
  9. open class KMAdvertisementShowView_iOS: KMAdvertisementBaseView {
  10. @IBOutlet var contentView: UIView!
  11. @IBOutlet weak var imageView: UIImageView!
  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. // MARK: 初始化
  31. public required init?(coder decoder: NSCoder) {
  32. super.init(coder: decoder)
  33. initContentView()
  34. setup()
  35. }
  36. public override init(frame: CGRect) {
  37. super.init(frame: frame)
  38. initContentView()
  39. setup()
  40. }
  41. private func initContentView() {
  42. //绑定xib
  43. let resource = UINib(nibName: String(describing: self.classForCoder.self),
  44. bundle: Bundle(for: self.classForCoder.self))
  45. resource.instantiate(withOwner: self)
  46. addSubview(contentView)
  47. contentView.translatesAutoresizingMaskIntoConstraints = false
  48. NSLayoutConstraint.activate([
  49. contentView.topAnchor.constraint(equalTo: topAnchor),
  50. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  51. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  52. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  53. contentView.updateConstraintsIfNeeded()
  54. }
  55. func setup() {
  56. let tap = UITapGestureRecognizer(target: self, action: #selector(buttonAction))
  57. self.imageView.addGestureRecognizer(tap)
  58. self.imageView.isUserInteractionEnabled = true
  59. }
  60. func reloadData() {
  61. let url = URL(string: self.data?.first?.content.first?.imageURL?.normal?.en ?? "")
  62. self.imageView.kf.setImage(with: url)
  63. }
  64. @objc func buttonAction() {
  65. let content = self.data?.first?.content.first
  66. if actionCompletion != nil {
  67. actionCompletion!(content!)
  68. }
  69. }
  70. }
  71. extension KMAdvertisementShowView_iOS: KMAdvertisementTableViewPresenterDelegate {
  72. func showData(presenter: KMAdvertisementTableViewPresenter, data: Array<KMAdvertisementModel.Section>) {
  73. self.data = data
  74. self.reloadData()
  75. }
  76. }