JXSegmentedTitleImageDataSource.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // JXSegmentedTitleImageDataSource.swift
  3. // JXSegmentedView
  4. //
  5. // Created by jiaxin on 2018/12/29.
  6. // Copyright © 2018 jiaxin. All rights reserved.
  7. //
  8. import UIKit
  9. public enum JXSegmentedTitleImageType {
  10. case topImage
  11. case leftImage
  12. case bottomImage
  13. case rightImage
  14. case onlyImage
  15. case onlyTitle
  16. }
  17. public typealias LoadImageClosure = ((UIImageView, String) -> Void)
  18. open class JXSegmentedTitleImageDataSource: JXSegmentedTitleDataSource {
  19. open var titleImageType: JXSegmentedTitleImageType = .rightImage
  20. /// 数量需要和item的数量保持一致。可以是ImageName或者图片网络地址
  21. open var normalImageInfos: [String]?
  22. /// 数量需要和item的数量保持一致。可以是ImageName或者图片网络地址。如果不赋值,选中时就不会处理图片切换。
  23. open var selectedImageInfos: [String]?
  24. /// 内部默认通过UIImage(named:)加载图片。如果传递的是图片网络地址或者想自己处理图片加载逻辑,可以通过该闭包处理。
  25. open var loadImageClosure: LoadImageClosure?
  26. /// 图片尺寸
  27. open var imageSize: CGSize = CGSize(width: 20, height: 20)
  28. /// title和image之间的间隔
  29. open var titleImageSpacing: CGFloat = 5
  30. /// 是否开启图片缩放
  31. open var isImageZoomEnabled: Bool = false
  32. /// 图片缩放选中时的scale
  33. open var imageSelectedZoomScale: CGFloat = 1.2
  34. open override func preferredItemModelInstance() -> JXSegmentedBaseItemModel {
  35. return JXSegmentedTitleImageItemModel()
  36. }
  37. open override func preferredRefreshItemModel(_ itemModel: JXSegmentedBaseItemModel, at index: Int, selectedIndex: Int) {
  38. super.preferredRefreshItemModel(itemModel, at: index, selectedIndex: selectedIndex)
  39. guard let itemModel = itemModel as? JXSegmentedTitleImageItemModel else {
  40. return
  41. }
  42. itemModel.titleImageType = titleImageType
  43. itemModel.normalImageInfo = normalImageInfos?[index]
  44. itemModel.selectedImageInfo = selectedImageInfos?[index]
  45. itemModel.loadImageClosure = loadImageClosure
  46. itemModel.imageSize = imageSize
  47. itemModel.isImageZoomEnabled = isImageZoomEnabled
  48. itemModel.imageNormalZoomScale = 1
  49. itemModel.imageSelectedZoomScale = imageSelectedZoomScale
  50. itemModel.titleImageSpacing = titleImageSpacing
  51. if index == selectedIndex {
  52. itemModel.imageCurrentZoomScale = itemModel.imageSelectedZoomScale
  53. }else {
  54. itemModel.imageCurrentZoomScale = itemModel.imageNormalZoomScale
  55. }
  56. }
  57. open override func preferredSegmentedView(_ segmentedView: JXSegmentedView, widthForItemAt index: Int) -> CGFloat {
  58. var width = super.preferredSegmentedView(segmentedView, widthForItemAt: index)
  59. if itemWidth == JXSegmentedViewAutomaticDimension {
  60. switch titleImageType {
  61. case .leftImage, .rightImage:
  62. width += titleImageSpacing + imageSize.width
  63. case .topImage, .bottomImage:
  64. width = max(itemWidth, imageSize.width)
  65. case .onlyImage:
  66. width = imageSize.width
  67. case .onlyTitle:
  68. break
  69. }
  70. }
  71. return width
  72. }
  73. public override func segmentedView(_ segmentedView: JXSegmentedView, widthForItemContentAt index: Int) -> CGFloat {
  74. var width = super.segmentedView(segmentedView, widthForItemContentAt: index)
  75. switch titleImageType {
  76. case .leftImage, .rightImage:
  77. width += titleImageSpacing + imageSize.width
  78. case .topImage, .bottomImage:
  79. width = max(itemWidth, imageSize.width)
  80. case .onlyImage:
  81. width = imageSize.width
  82. case .onlyTitle:
  83. break
  84. }
  85. return width
  86. }
  87. //MARK: - JXSegmentedViewDataSource
  88. open override func registerCellClass(in segmentedView: JXSegmentedView) {
  89. segmentedView.collectionView.register(JXSegmentedTitleImageCell.self, forCellWithReuseIdentifier: "cell")
  90. }
  91. open override func segmentedView(_ segmentedView: JXSegmentedView, cellForItemAt index: Int) -> JXSegmentedBaseCell {
  92. let cell = segmentedView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
  93. return cell
  94. }
  95. open override func refreshItemModel(_ segmentedView: JXSegmentedView, leftItemModel: JXSegmentedBaseItemModel, rightItemModel: JXSegmentedBaseItemModel, percent: CGFloat) {
  96. super.refreshItemModel(segmentedView, leftItemModel: leftItemModel, rightItemModel: rightItemModel, percent: percent)
  97. guard let leftModel = leftItemModel as? JXSegmentedTitleImageItemModel, let rightModel = rightItemModel as? JXSegmentedTitleImageItemModel else {
  98. return
  99. }
  100. if isImageZoomEnabled && isItemTransitionEnabled {
  101. leftModel.imageCurrentZoomScale = JXSegmentedViewTool.interpolate(from: imageSelectedZoomScale, to: 1, percent: CGFloat(percent))
  102. rightModel.imageCurrentZoomScale = JXSegmentedViewTool.interpolate(from: 1, to: imageSelectedZoomScale, percent: CGFloat(percent))
  103. }
  104. }
  105. open override func refreshItemModel(_ segmentedView: JXSegmentedView, currentSelectedItemModel: JXSegmentedBaseItemModel, willSelectedItemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
  106. super.refreshItemModel(segmentedView, currentSelectedItemModel: currentSelectedItemModel, willSelectedItemModel: willSelectedItemModel, selectedType: selectedType)
  107. guard let myCurrentSelectedItemModel = currentSelectedItemModel as? JXSegmentedTitleImageItemModel, let myWillSelectedItemModel = willSelectedItemModel as? JXSegmentedTitleImageItemModel else {
  108. return
  109. }
  110. myCurrentSelectedItemModel.imageCurrentZoomScale = myCurrentSelectedItemModel.imageNormalZoomScale
  111. myWillSelectedItemModel.imageCurrentZoomScale = myWillSelectedItemModel.imageSelectedZoomScale
  112. }
  113. }