JXSegmentedBaseCell.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // JXSegmentedBaseCell.swift
  3. // JXSegmentedView
  4. //
  5. // Created by jiaxin on 2018/12/26.
  6. // Copyright © 2018 jiaxin. All rights reserved.
  7. //
  8. import UIKit
  9. public typealias JXSegmentedCellSelectedAnimationClosure = (CGFloat)->()
  10. open class JXSegmentedBaseCell: UICollectionViewCell, JXSegmentedViewRTLCompatible {
  11. open var itemModel: JXSegmentedBaseItemModel?
  12. open var animator: JXSegmentedAnimator?
  13. private var selectedAnimationClosureArray = [JXSegmentedCellSelectedAnimationClosure]()
  14. deinit {
  15. animator?.stop()
  16. }
  17. open override func prepareForReuse() {
  18. super.prepareForReuse()
  19. animator?.stop()
  20. animator = nil
  21. }
  22. public override init(frame: CGRect) {
  23. super.init(frame: frame)
  24. commonInit()
  25. }
  26. required public init?(coder aDecoder: NSCoder) {
  27. super.init(coder: aDecoder)
  28. commonInit()
  29. }
  30. open func commonInit() {
  31. if segmentedViewShouldRTLLayout() {
  32. segmentedView(horizontalFlipForView: self)
  33. segmentedView(horizontalFlipForView: contentView)
  34. }
  35. }
  36. open func canStartSelectedAnimation(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) -> Bool {
  37. var isSelectedAnimatable = false
  38. if itemModel.isSelectedAnimable {
  39. if selectedType == .scroll {
  40. //滚动选中且没有开启左右过渡,允许动画
  41. if !itemModel.isItemTransitionEnabled {
  42. isSelectedAnimatable = true
  43. }
  44. }else if selectedType == .click || selectedType == .code {
  45. //点击和代码选中,允许动画
  46. isSelectedAnimatable = true
  47. }
  48. }
  49. return isSelectedAnimatable
  50. }
  51. open func appendSelectedAnimationClosure(closure: @escaping JXSegmentedCellSelectedAnimationClosure) {
  52. selectedAnimationClosureArray.append(closure)
  53. }
  54. open func startSelectedAnimationIfNeeded(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
  55. if itemModel.isSelectedAnimable && canStartSelectedAnimation(itemModel: itemModel, selectedType: selectedType) {
  56. //需要更新isTransitionAnimating,用于处理在过滤时,禁止响应点击,避免界面异常。
  57. itemModel.isTransitionAnimating = true
  58. animator?.progressClosure = {[weak self] (percent) in
  59. guard self != nil else {
  60. return
  61. }
  62. for closure in self!.selectedAnimationClosureArray {
  63. closure(percent)
  64. }
  65. }
  66. animator?.completedClosure = {[weak self] in
  67. itemModel.isTransitionAnimating = false
  68. self?.selectedAnimationClosureArray.removeAll()
  69. }
  70. animator?.start()
  71. }
  72. }
  73. open func reloadData(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
  74. self.itemModel = itemModel
  75. if itemModel.isSelectedAnimable {
  76. selectedAnimationClosureArray.removeAll()
  77. if canStartSelectedAnimation(itemModel: itemModel, selectedType: selectedType) {
  78. animator = JXSegmentedAnimator()
  79. animator?.duration = itemModel.selectedAnimationDuration
  80. }else {
  81. animator?.stop()
  82. animator = nil
  83. }
  84. }
  85. }
  86. open override var isSelected: Bool {
  87. didSet {
  88. setSelectedStyle(isSelected: isSelected)
  89. }
  90. }
  91. open override var isHighlighted: Bool {
  92. didSet {
  93. setSelectedStyle(isSelected: isHighlighted)
  94. }
  95. }
  96. func setSelectedStyle(isSelected: Bool) {
  97. }
  98. }