MJRefreshBackNormalFooter.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // MJRefreshBackNormalFooter.m
  3. // MJRefresh
  4. //
  5. // Created by MJ Lee on 15/4/24.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJRefreshBackNormalFooter.h"
  9. #import "NSBundle+MJRefresh.h"
  10. #import "UIView+MJExtension.h"
  11. @interface MJRefreshBackNormalFooter()
  12. {
  13. __unsafe_unretained UIImageView *_arrowView;
  14. }
  15. @property (weak, nonatomic) UIActivityIndicatorView *loadingView;
  16. @end
  17. @implementation MJRefreshBackNormalFooter
  18. #pragma mark - 懒加载子控件
  19. - (UIImageView *)arrowView
  20. {
  21. if (!_arrowView) {
  22. UIImageView *arrowView = [[UIImageView alloc] initWithImage:[NSBundle mj_arrowImage]];
  23. [self addSubview:_arrowView = arrowView];
  24. }
  25. return _arrowView;
  26. }
  27. - (UIActivityIndicatorView *)loadingView
  28. {
  29. if (!_loadingView) {
  30. UIActivityIndicatorView *loadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:_activityIndicatorViewStyle];
  31. loadingView.hidesWhenStopped = YES;
  32. [self addSubview:_loadingView = loadingView];
  33. }
  34. return _loadingView;
  35. }
  36. - (void)setActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)activityIndicatorViewStyle
  37. {
  38. _activityIndicatorViewStyle = activityIndicatorViewStyle;
  39. [self.loadingView removeFromSuperview];
  40. self.loadingView = nil;
  41. [self setNeedsLayout];
  42. }
  43. #pragma mark - 重写父类的方法
  44. - (void)prepare
  45. {
  46. [super prepare];
  47. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  48. if (@available(iOS 13.0, *)) {
  49. _activityIndicatorViewStyle = UIActivityIndicatorViewStyleMedium;
  50. return;
  51. }
  52. #endif
  53. _activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
  54. }
  55. - (void)placeSubviews
  56. {
  57. [super placeSubviews];
  58. // 箭头的中心点
  59. CGFloat arrowCenterX = self.mj_w * 0.5;
  60. if (!self.stateLabel.hidden) {
  61. arrowCenterX -= self.labelLeftInset + self.stateLabel.mj_textWidth * 0.5;
  62. }
  63. CGFloat arrowCenterY = self.mj_h * 0.5;
  64. CGPoint arrowCenter = CGPointMake(arrowCenterX, arrowCenterY);
  65. // 箭头
  66. if (self.arrowView.constraints.count == 0) {
  67. self.arrowView.mj_size = self.arrowView.image.size;
  68. self.arrowView.center = arrowCenter;
  69. }
  70. // 圈圈
  71. if (self.loadingView.constraints.count == 0) {
  72. self.loadingView.center = arrowCenter;
  73. }
  74. self.arrowView.tintColor = self.stateLabel.textColor;
  75. }
  76. - (void)setState:(MJRefreshState)state
  77. {
  78. MJRefreshCheckState
  79. // 根据状态做事情
  80. if (state == MJRefreshStateIdle) {
  81. if (oldState == MJRefreshStateRefreshing) {
  82. self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
  83. [UIView animateWithDuration:self.slowAnimationDuration animations:^{
  84. self.loadingView.alpha = 0.0;
  85. } completion:^(BOOL finished) {
  86. // 防止动画结束后,状态已经不是MJRefreshStateIdle
  87. if (self.state != MJRefreshStateIdle) return;
  88. self.loadingView.alpha = 1.0;
  89. [self.loadingView stopAnimating];
  90. self.arrowView.hidden = NO;
  91. }];
  92. } else {
  93. self.arrowView.hidden = NO;
  94. [self.loadingView stopAnimating];
  95. [UIView animateWithDuration:self.fastAnimationDuration animations:^{
  96. self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
  97. }];
  98. }
  99. } else if (state == MJRefreshStatePulling) {
  100. self.arrowView.hidden = NO;
  101. [self.loadingView stopAnimating];
  102. [UIView animateWithDuration:self.fastAnimationDuration animations:^{
  103. self.arrowView.transform = CGAffineTransformIdentity;
  104. }];
  105. } else if (state == MJRefreshStateRefreshing) {
  106. self.arrowView.hidden = YES;
  107. [self.loadingView startAnimating];
  108. } else if (state == MJRefreshStateNoMoreData) {
  109. self.arrowView.hidden = YES;
  110. [self.loadingView stopAnimating];
  111. }
  112. }
  113. @end