JXSegmentedTitleImageDataSource.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 itemWidth = super.preferredSegmentedView(segmentedView, widthForItemAt: index)
  59. if itemContentWidth == JXSegmentedViewAutomaticDimension {
  60. switch titleImageType {
  61. case .leftImage, .rightImage:
  62. itemWidth += titleImageSpacing + imageSize.width
  63. case .topImage, .bottomImage:
  64. itemWidth = max(itemWidth, imageSize.width)
  65. case .onlyImage:
  66. itemWidth = imageSize.width
  67. case .onlyTitle:
  68. break
  69. }
  70. }
  71. return itemWidth
  72. }
  73. //MARK: - JXSegmentedViewDataSource
  74. open override func registerCellClass(in segmentedView: JXSegmentedView) {
  75. segmentedView.collectionView.register(JXSegmentedTitleImageCell.self, forCellWithReuseIdentifier: "cell")
  76. }
  77. open override func segmentedView(_ segmentedView: JXSegmentedView, cellForItemAt index: Int) -> JXSegmentedBaseCell {
  78. let cell = segmentedView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
  79. return cell
  80. }
  81. open override func refreshItemModel(_ segmentedView: JXSegmentedView, leftItemModel: JXSegmentedBaseItemModel, rightItemModel: JXSegmentedBaseItemModel, percent: CGFloat) {
  82. super.refreshItemModel(segmentedView, leftItemModel: leftItemModel, rightItemModel: rightItemModel, percent: percent)
  83. guard let leftModel = leftItemModel as? JXSegmentedTitleImageItemModel, let rightModel = rightItemModel as? JXSegmentedTitleImageItemModel else {
  84. return
  85. }
  86. if isImageZoomEnabled && isItemTransitionEnabled {
  87. leftModel.imageCurrentZoomScale = JXSegmentedViewTool.interpolate(from: imageSelectedZoomScale, to: 1, percent: CGFloat(percent))
  88. rightModel.imageCurrentZoomScale = JXSegmentedViewTool.interpolate(from: 1, to: imageSelectedZoomScale, percent: CGFloat(percent))
  89. }
  90. }
  91. open override func refreshItemModel(_ segmentedView: JXSegmentedView, currentSelectedItemModel: JXSegmentedBaseItemModel, willSelectedItemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
  92. super.refreshItemModel(segmentedView, currentSelectedItemModel: currentSelectedItemModel, willSelectedItemModel: willSelectedItemModel, selectedType: selectedType)
  93. guard let myCurrentSelectedItemModel = currentSelectedItemModel as? JXSegmentedTitleImageItemModel, let myWillSelectedItemModel = willSelectedItemModel as? JXSegmentedTitleImageItemModel else {
  94. return
  95. }
  96. myCurrentSelectedItemModel.imageCurrentZoomScale = myCurrentSelectedItemModel.imageNormalZoomScale
  97. myWillSelectedItemModel.imageCurrentZoomScale = myWillSelectedItemModel.imageSelectedZoomScale
  98. }
  99. }