MJRefreshComponent.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // 代码地址: https://github.com/CoderMJLee/MJRefresh
  2. // MJRefreshComponent.h
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 15/3/4.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. // 刷新控件的基类
  8. #import <UIKit/UIKit.h>
  9. #import "MJRefreshConst.h"
  10. #import "UIView+MJExtension.h"
  11. #import "UIScrollView+MJExtension.h"
  12. #import "UIScrollView+MJRefresh.h"
  13. #import "NSBundle+MJRefresh.h"
  14. NS_ASSUME_NONNULL_BEGIN
  15. /** 刷新控件的状态 */
  16. typedef NS_ENUM(NSInteger, MJRefreshState) {
  17. /** 普通闲置状态 */
  18. MJRefreshStateIdle = 1,
  19. /** 松开就可以进行刷新的状态 */
  20. MJRefreshStatePulling,
  21. /** 正在刷新中的状态 */
  22. MJRefreshStateRefreshing,
  23. /** 即将刷新的状态 */
  24. MJRefreshStateWillRefresh,
  25. /** 所有数据加载完毕,没有更多的数据了 */
  26. MJRefreshStateNoMoreData
  27. };
  28. /** 进入刷新状态的回调 */
  29. typedef void (^MJRefreshComponentRefreshingBlock)(void) MJRefreshDeprecated("first deprecated in 3.3.0 - Use `MJRefreshComponentAction` instead");
  30. /** 开始刷新后的回调(进入刷新状态后的回调) */
  31. typedef void (^MJRefreshComponentBeginRefreshingCompletionBlock)(void) MJRefreshDeprecated("first deprecated in 3.3.0 - Use `MJRefreshComponentAction` instead");
  32. /** 结束刷新后的回调 */
  33. typedef void (^MJRefreshComponentEndRefreshingCompletionBlock)(void) MJRefreshDeprecated("first deprecated in 3.3.0 - Use `MJRefreshComponentAction` instead");
  34. /** 刷新用到的回调类型 */
  35. typedef void (^MJRefreshComponentAction)(void);
  36. /** 刷新控件的基类 */
  37. @interface MJRefreshComponent : UIView
  38. {
  39. /** 记录scrollView刚开始的inset */
  40. UIEdgeInsets _scrollViewOriginalInset;
  41. /** 父控件 */
  42. __weak UIScrollView *_scrollView;
  43. }
  44. #pragma mark - 刷新动画时间控制
  45. /** 快速动画时间(一般用在刷新开始的回弹动画), 默认 0.25 */
  46. @property (nonatomic) NSTimeInterval fastAnimationDuration;
  47. /** 慢速动画时间(一般用在刷新结束后的回弹动画), 默认 0.4*/
  48. @property (nonatomic) NSTimeInterval slowAnimationDuration;
  49. /** 关闭全部默认动画效果, 可以简单粗暴地解决 CollectionView 的回弹动画 bug */
  50. - (instancetype)setAnimationDisabled;
  51. #pragma mark - 刷新回调
  52. /** 正在刷新的回调 */
  53. @property (copy, nonatomic, nullable) MJRefreshComponentAction refreshingBlock;
  54. /** 设置回调对象和回调方法 */
  55. - (void)setRefreshingTarget:(id)target refreshingAction:(SEL)action;
  56. /** 回调对象 */
  57. @property (weak, nonatomic) id refreshingTarget;
  58. /** 回调方法 */
  59. @property (assign, nonatomic) SEL refreshingAction;
  60. /** 触发回调(交给子类去调用) */
  61. - (void)executeRefreshingCallback;
  62. #pragma mark - 刷新状态控制
  63. /** 进入刷新状态 */
  64. - (void)beginRefreshing;
  65. - (void)beginRefreshingWithCompletionBlock:(void (^)(void))completionBlock;
  66. /** 开始刷新后的回调(进入刷新状态后的回调) */
  67. @property (copy, nonatomic, nullable) MJRefreshComponentAction beginRefreshingCompletionBlock;
  68. /** 带动画的结束刷新的回调 */
  69. @property (copy, nonatomic, nullable) MJRefreshComponentAction endRefreshingAnimateCompletionBlock MJRefreshDeprecated("first deprecated in 3.3.0 - Use `endRefreshingAnimationBeginAction` instead");
  70. @property (copy, nonatomic, nullable) MJRefreshComponentAction endRefreshingAnimationBeginAction;
  71. /** 结束刷新的回调 */
  72. @property (copy, nonatomic, nullable) MJRefreshComponentAction endRefreshingCompletionBlock;
  73. /** 结束刷新状态 */
  74. - (void)endRefreshing;
  75. - (void)endRefreshingWithCompletionBlock:(void (^)(void))completionBlock;
  76. /** 是否正在刷新 */
  77. @property (assign, nonatomic, readonly, getter=isRefreshing) BOOL refreshing;
  78. /** 刷新状态 一般交给子类内部实现 */
  79. @property (assign, nonatomic) MJRefreshState state;
  80. #pragma mark - 交给子类去访问
  81. /** 记录scrollView刚开始的inset */
  82. @property (assign, nonatomic, readonly) UIEdgeInsets scrollViewOriginalInset;
  83. /** 父控件 */
  84. @property (weak, nonatomic, readonly) UIScrollView *scrollView;
  85. #pragma mark - 交给子类们去实现
  86. /** 初始化 */
  87. - (void)prepare NS_REQUIRES_SUPER;
  88. /** 摆放子控件frame */
  89. - (void)placeSubviews NS_REQUIRES_SUPER;
  90. /** 当scrollView的contentOffset发生改变的时候调用 */
  91. - (void)scrollViewContentOffsetDidChange:(nullable NSDictionary *)change NS_REQUIRES_SUPER;
  92. /** 当scrollView的contentSize发生改变的时候调用 */
  93. - (void)scrollViewContentSizeDidChange:(nullable NSDictionary *)change NS_REQUIRES_SUPER;
  94. /** 当scrollView的拖拽状态发生改变的时候调用 */
  95. - (void)scrollViewPanStateDidChange:(nullable NSDictionary *)change NS_REQUIRES_SUPER;
  96. #pragma mark - 其他
  97. /** 拉拽的百分比(交给子类重写) */
  98. @property (assign, nonatomic) CGFloat pullingPercent;
  99. /** 根据拖拽比例自动切换透明度 */
  100. @property (assign, nonatomic, getter=isAutoChangeAlpha) BOOL autoChangeAlpha MJRefreshDeprecated("请使用automaticallyChangeAlpha属性");
  101. /** 根据拖拽比例自动切换透明度 */
  102. @property (assign, nonatomic, getter=isAutomaticallyChangeAlpha) BOOL automaticallyChangeAlpha;
  103. @end
  104. @interface UILabel(MJRefresh)
  105. + (instancetype)mj_label;
  106. - (CGFloat)mj_textWidth;
  107. @end
  108. @interface MJRefreshComponent (ChainingGrammar)
  109. #pragma mark - <<< 为 Swift 扩展链式语法 >>> -
  110. /// 自动变化透明度
  111. - (instancetype)autoChangeTransparency:(BOOL)isAutoChange;
  112. /// 刷新开始后立即调用的回调
  113. - (instancetype)afterBeginningAction:(MJRefreshComponentAction)action;
  114. /// 刷新动画开始后立即调用的回调
  115. - (instancetype)endingAnimationBeginningAction:(MJRefreshComponentAction)action;
  116. /// 刷新结束后立即调用的回调
  117. - (instancetype)afterEndingAction:(MJRefreshComponentAction)action;
  118. /// 需要子类必须实现
  119. /// @param scrollView 赋值给的 ScrollView 的 Header/Footer/Trailer
  120. - (instancetype)assignTo:(UIScrollView *)scrollView;
  121. @end
  122. NS_ASSUME_NONNULL_END