MJRefreshBackNormalFooter.m 3.7 KB

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