KMAdvertisementShowView_iOS.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. var timer: Timer?
  13. var index: Int = 0
  14. //内部使用数据
  15. fileprivate var data: [KMAdvertisementModelItem] = []
  16. /**
  17. @abstract 外部传入数据
  18. @param inputData 文件路劲
  19. */
  20. open var inputData: KMAdvertisementModel! {
  21. didSet {
  22. self.reloadData()
  23. }
  24. }
  25. convenience init (data: KMAdvertisementModel, superView: UIView) {
  26. self.init(frame: superView.bounds)
  27. superView.addSubview(self)
  28. self.autoresizingMask = [.flexibleHeight , .flexibleWidth]
  29. self.inputData = data
  30. self.reloadData()
  31. }
  32. // MARK: 初始化
  33. public required init?(coder decoder: NSCoder) {
  34. super.init(coder: decoder)
  35. initContentView()
  36. setup()
  37. }
  38. public override init(frame: CGRect) {
  39. super.init(frame: frame)
  40. initContentView()
  41. setup()
  42. }
  43. private func initContentView() {
  44. //绑定xib
  45. let resource = UINib(nibName: String(describing: self.classForCoder.self),
  46. bundle: Bundle(for: self.classForCoder.self))
  47. resource.instantiate(withOwner: self)
  48. addSubview(contentView)
  49. contentView.translatesAutoresizingMaskIntoConstraints = false
  50. NSLayoutConstraint.activate([
  51. contentView.topAnchor.constraint(equalTo: topAnchor),
  52. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  53. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  54. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  55. contentView.updateConstraintsIfNeeded()
  56. }
  57. func setup() {
  58. let tap = UITapGestureRecognizer(target: self, action: #selector(buttonAction))
  59. self.imageView.addGestureRecognizer(tap)
  60. self.imageView.isUserInteractionEnabled = true
  61. self.addTimer()
  62. }
  63. func reloadData() {
  64. self.data.removeAll()
  65. let array = inputData.content!
  66. for section in array {
  67. for item in section.content! {
  68. self.data.append(item)
  69. }
  70. }
  71. if self.data.count == 0 {
  72. self.removeTimer()
  73. self.removeFromSuperview()
  74. }
  75. self.recordTime()
  76. }
  77. //MARK: 定时器
  78. func addTimer() {
  79. self.removeTimer()
  80. timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector (recordTime),
  81. userInfo: nil, repeats: true)
  82. if let curTimer: Timer = timer {
  83. RunLoop.main.add(curTimer, forMode: .common)
  84. }
  85. }
  86. func removeTimer() {
  87. if (timer != nil) {
  88. timer?.invalidate ()
  89. timer = nil
  90. }
  91. }
  92. @objc func recordTime () {
  93. if self.data.count != 0 {
  94. index += 1
  95. if index >= self.data.count {
  96. index = 0
  97. }
  98. if self.data.count != 0 {
  99. let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: self.data[index].imageURL!, highlight: false))
  100. self.imageView.kf.setImage(with: url)
  101. }
  102. }
  103. }
  104. @objc func buttonAction() {
  105. if self.data.count > index {
  106. let content = self.data[index]
  107. if actionCompletion != nil {
  108. actionCompletion!(.tap, content)
  109. }
  110. }
  111. }
  112. }