JXCategoryBaseCell.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // JXCategoryBaseCell.m
  3. // UI系列测试
  4. //
  5. // Created by jiaxin on 2018/3/15.
  6. // Copyright © 2018年 jiaxin. All rights reserved.
  7. //
  8. #import "JXCategoryBaseCell.h"
  9. #import "RTLManager.h"
  10. @interface JXCategoryBaseCell ()
  11. @property (nonatomic, strong) JXCategoryBaseCellModel *cellModel;
  12. @property (nonatomic, strong) JXCategoryViewAnimator *animator;
  13. @property (nonatomic, strong) NSMutableArray <JXCategoryCellSelectedAnimationBlock> *animationBlockArray;
  14. @end
  15. @implementation JXCategoryBaseCell
  16. #pragma mark - Initialize
  17. - (void)dealloc {
  18. [self.animator stop];
  19. }
  20. - (void)prepareForReuse {
  21. [super prepareForReuse];
  22. [self.animator stop];
  23. }
  24. - (instancetype)initWithFrame:(CGRect)frame {
  25. self = [super initWithFrame:frame];
  26. if (self) {
  27. [self initializeViews];
  28. }
  29. return self;
  30. }
  31. - (instancetype)initWithCoder:(NSCoder *)coder {
  32. self = [super initWithCoder:coder];
  33. if (self) {
  34. [self initializeViews];
  35. }
  36. return self;
  37. }
  38. #pragma mark - Public
  39. - (void)initializeViews {
  40. _animationBlockArray = [NSMutableArray array];
  41. [RTLManager horizontalFlipViewIfNeeded:self];
  42. [RTLManager horizontalFlipViewIfNeeded:self.contentView];
  43. }
  44. - (void)reloadData:(JXCategoryBaseCellModel *)cellModel {
  45. self.cellModel = cellModel;
  46. if (cellModel.isSelectedAnimationEnabled) {
  47. [self.animationBlockArray removeLastObject];
  48. if ([self checkCanStartSelectedAnimation:cellModel]) {
  49. self.animator = [[JXCategoryViewAnimator alloc] init];
  50. self.animator.duration = cellModel.selectedAnimationDuration;
  51. } else {
  52. [self.animator stop];
  53. }
  54. }
  55. }
  56. - (BOOL)checkCanStartSelectedAnimation:(JXCategoryBaseCellModel *)cellModel {
  57. BOOL canStartSelectedAnimation = ((cellModel.selectedType == JXCategoryCellSelectedTypeCode) || (cellModel.selectedType == JXCategoryCellSelectedTypeClick));
  58. return canStartSelectedAnimation;
  59. }
  60. - (void)addSelectedAnimationBlock:(JXCategoryCellSelectedAnimationBlock)block {
  61. [self.animationBlockArray addObject:block];
  62. }
  63. - (void)startSelectedAnimationIfNeeded:(JXCategoryBaseCellModel *)cellModel {
  64. if (cellModel.isSelectedAnimationEnabled && [self checkCanStartSelectedAnimation:cellModel]) {
  65. // 需要更新 isTransitionAnimating,用于处理在过滤时,禁止响应点击,避免界面异常。
  66. cellModel.transitionAnimating = YES;
  67. __weak typeof(self)weakSelf = self;
  68. self.animator.progressCallback = ^(CGFloat percent) {
  69. for (JXCategoryCellSelectedAnimationBlock block in weakSelf.animationBlockArray) {
  70. block(percent);
  71. }
  72. };
  73. self.animator.completeCallback = ^{
  74. cellModel.transitionAnimating = NO;
  75. [weakSelf.animationBlockArray removeAllObjects];
  76. };
  77. [self.animator start];
  78. }
  79. }
  80. @end