JXSegmentedBaseCell.swift 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 {
  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. }
  21. public override init(frame: CGRect) {
  22. super.init(frame: frame)
  23. commonInit()
  24. }
  25. required public init?(coder aDecoder: NSCoder) {
  26. super.init(coder: aDecoder)
  27. commonInit()
  28. }
  29. open func commonInit() {
  30. }
  31. open func canStartSelectedAnimation(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) -> Bool {
  32. var isSelectedAnimatable = false
  33. if itemModel.isSelectedAnimable {
  34. if selectedType == .scroll {
  35. //滚动选中且没有开启左右过渡,允许动画
  36. if !itemModel.isItemTransitionEnabled {
  37. isSelectedAnimatable = true
  38. }
  39. }else if selectedType == .click || selectedType == .code {
  40. //点击和代码选中,允许动画
  41. isSelectedAnimatable = true
  42. }
  43. }
  44. return isSelectedAnimatable
  45. }
  46. open func appendSelectedAnimationClosure(closure: @escaping JXSegmentedCellSelectedAnimationClosure) {
  47. selectedAnimationClosureArray.append(closure)
  48. }
  49. open func startSelectedAnimationIfNeeded(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
  50. if itemModel.isSelectedAnimable && canStartSelectedAnimation(itemModel: itemModel, selectedType: selectedType) {
  51. //需要更新isTransitionAnimating,用于处理在过滤时,禁止响应点击,避免界面异常。
  52. itemModel.isTransitionAnimating = true
  53. animator?.progressClosure = {[weak self] (percent) in
  54. guard self != nil else {
  55. return
  56. }
  57. for closure in self!.selectedAnimationClosureArray {
  58. closure(percent)
  59. }
  60. }
  61. animator?.completedClosure = {[weak self] in
  62. itemModel.isTransitionAnimating = false
  63. self?.selectedAnimationClosureArray.removeAll()
  64. }
  65. animator?.start()
  66. }
  67. }
  68. open func reloadData(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
  69. self.itemModel = itemModel
  70. if itemModel.isSelectedAnimable {
  71. selectedAnimationClosureArray.removeAll()
  72. if canStartSelectedAnimation(itemModel: itemModel, selectedType: selectedType) {
  73. animator = JXSegmentedAnimator()
  74. animator?.duration = itemModel.selectedAnimationDuration
  75. }else {
  76. animator?.stop()
  77. }
  78. }
  79. }
  80. }