JXSegmentedTitleOrImageCell.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // JXSegmentedTitleOrImageCell.swift
  3. // JXSegmentedView
  4. //
  5. // Created by jiaxin on 2019/1/22.
  6. // Copyright © 2019 jiaxin. All rights reserved.
  7. //
  8. import UIKit
  9. open class JXSegmentedTitleOrImageCell: JXSegmentedTitleCell {
  10. public let imageView = UIImageView()
  11. private var currentImageInfo: String?
  12. open override func prepareForReuse() {
  13. super.prepareForReuse()
  14. currentImageInfo = nil
  15. }
  16. open override func commonInit() {
  17. super.commonInit()
  18. imageView.contentMode = .scaleAspectFit
  19. contentView.addSubview(imageView)
  20. }
  21. open override func layoutSubviews() {
  22. super.layoutSubviews()
  23. imageView.center = contentView.center
  24. }
  25. open override func reloadData(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
  26. super.reloadData(itemModel: itemModel, selectedType: selectedType )
  27. guard let myItemModel = itemModel as? JXSegmentedTitleOrImageItemModel else {
  28. return
  29. }
  30. if myItemModel.isSelected && myItemModel.selectedImageInfo != nil {
  31. titleLabel.isHidden = true
  32. imageView.isHidden = false
  33. }else {
  34. titleLabel.isHidden = false
  35. imageView.isHidden = true
  36. }
  37. imageView.bounds = CGRect(x: 0, y: 0, width: myItemModel.imageSize.width, height: myItemModel.imageSize.height)
  38. //因为`func reloadData(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType)`方法会回调多次,尤其是左右滚动的时候会调用无数次。如果每次都触发图片加载,会非常消耗性能。所以只会在图片发生了变化的时候,才进行图片加载。
  39. if myItemModel.isSelected &&
  40. myItemModel.selectedImageInfo != nil &&
  41. myItemModel.selectedImageInfo != currentImageInfo {
  42. currentImageInfo = myItemModel.selectedImageInfo
  43. if myItemModel.loadImageClosure != nil {
  44. myItemModel.loadImageClosure!(imageView, myItemModel.selectedImageInfo!)
  45. }else {
  46. imageView.image = UIImage(named: myItemModel.selectedImageInfo!)
  47. }
  48. }
  49. setNeedsLayout()
  50. }
  51. open override func preferredTitleZoomAnimateClosure(itemModel: JXSegmentedTitleItemModel, baseScale: CGFloat) -> JXSegmentedCellSelectedAnimationClosure {
  52. guard let myItemModel = itemModel as? JXSegmentedTitleOrImageItemModel else {
  53. return super.preferredTitleZoomAnimateClosure(itemModel: itemModel, baseScale: baseScale)
  54. }
  55. if myItemModel.selectedImageInfo == nil && myItemModel.isSelected {
  56. //当前item没有选中图片且是将要选中的时候才做动画
  57. return super.preferredTitleZoomAnimateClosure(itemModel: itemModel, baseScale: baseScale)
  58. }else {
  59. let closure: JXSegmentedCellSelectedAnimationClosure = {[weak self] (percent) in
  60. if itemModel.isSelected {
  61. //将要选中
  62. itemModel.titleCurrentZoomScale = itemModel.titleSelectedZoomScale
  63. }else {
  64. //将要取消选中
  65. itemModel.titleCurrentZoomScale = itemModel.titleNormalZoomScale
  66. }
  67. let currentTransform = CGAffineTransform(scaleX: baseScale*itemModel.titleCurrentZoomScale, y: baseScale*itemModel.titleCurrentZoomScale)
  68. self?.titleLabel.transform = currentTransform
  69. self?.maskTitleLabel.transform = currentTransform
  70. }
  71. //手动调用closure,更新到最新状态
  72. closure(0)
  73. return closure
  74. }
  75. }
  76. open override func preferredTitleStrokeWidthAnimateClosure(itemModel: JXSegmentedTitleItemModel, attriText: NSMutableAttributedString) -> JXSegmentedCellSelectedAnimationClosure {
  77. guard let myItemModel = itemModel as? JXSegmentedTitleOrImageItemModel else {
  78. return super.preferredTitleStrokeWidthAnimateClosure(itemModel: itemModel, attriText: attriText)
  79. }
  80. if myItemModel.selectedImageInfo == nil && myItemModel.isSelected {
  81. //当前item没有选中图片且是将要选中的时候才做动画
  82. return super.preferredTitleStrokeWidthAnimateClosure(itemModel: itemModel, attriText: attriText)
  83. }else {
  84. let closure: JXSegmentedCellSelectedAnimationClosure = {[weak self] (percent) in
  85. if itemModel.isSelected {
  86. //将要选中
  87. itemModel.titleCurrentStrokeWidth = itemModel.titleSelectedStrokeWidth
  88. }else {
  89. //将要取消选中
  90. itemModel.titleCurrentStrokeWidth = itemModel.titleNormalStrokeWidth
  91. }
  92. attriText.addAttributes([NSAttributedString.Key.strokeWidth: itemModel.titleCurrentStrokeWidth], range: NSRange(location: 0, length: attriText.string.count))
  93. self?.titleLabel.attributedText = attriText
  94. }
  95. //手动调用closure,更新到最新状态
  96. closure(0)
  97. return closure
  98. }
  99. }
  100. open override func preferredTitleColorAnimateClosure(itemModel: JXSegmentedTitleItemModel) -> JXSegmentedCellSelectedAnimationClosure {
  101. guard let myItemModel = itemModel as? JXSegmentedTitleOrImageItemModel else {
  102. return super.preferredTitleColorAnimateClosure(itemModel: itemModel)
  103. }
  104. if myItemModel.selectedImageInfo == nil && myItemModel.isSelected {
  105. //当前item没有选中图片且是将要选中的时候才做动画
  106. return super.preferredTitleColorAnimateClosure(itemModel: itemModel)
  107. }else {
  108. let closure: JXSegmentedCellSelectedAnimationClosure = {[weak self] (percent) in
  109. if itemModel.isSelected {
  110. //将要选中
  111. itemModel.titleCurrentColor = itemModel.titleSelectedColor
  112. }else {
  113. //将要取消选中
  114. itemModel.titleCurrentColor = itemModel.titleNormalColor
  115. }
  116. self?.titleLabel.textColor = itemModel.titleCurrentColor
  117. }
  118. //手动调用closure,更新到最新状态
  119. closure(0)
  120. return closure
  121. }
  122. }
  123. }