MJRefreshNormalHeader.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // MJRefreshNormalHeader.m
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 15/4/24.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJRefreshNormalHeader.h"
  9. #import "NSBundle+MJRefresh.h"
  10. @interface MJRefreshNormalHeader()
  11. {
  12. __unsafe_unretained UIImageView *_arrowView;
  13. }
  14. @property (weak, nonatomic) UIActivityIndicatorView *loadingView;
  15. @end
  16. @implementation MJRefreshNormalHeader
  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. #pragma mark - 公共方法
  36. - (void)setActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)activityIndicatorViewStyle
  37. {
  38. _activityIndicatorViewStyle = activityIndicatorViewStyle;
  39. self.loadingView = nil;
  40. [self setNeedsLayout];
  41. }
  42. #pragma mark - 重写父类的方法
  43. - (void)prepare
  44. {
  45. [super prepare];
  46. self.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
  47. }
  48. - (void)placeSubviews
  49. {
  50. [super placeSubviews];
  51. // 箭头的中心点
  52. CGFloat arrowCenterX = self.mj_w * 0.5;
  53. if (!self.stateLabel.hidden) {
  54. CGFloat stateWidth = self.stateLabel.mj_textWith;
  55. CGFloat timeWidth = 0.0;
  56. if (!self.lastUpdatedTimeLabel.hidden) {
  57. timeWidth = self.lastUpdatedTimeLabel.mj_textWith;
  58. }
  59. CGFloat textWidth = MAX(stateWidth, timeWidth);
  60. arrowCenterX -= textWidth / 2 + self.labelLeftInset;
  61. }
  62. CGFloat arrowCenterY = self.mj_h * 0.5;
  63. CGPoint arrowCenter = CGPointMake(arrowCenterX, arrowCenterY);
  64. // 箭头
  65. if (self.arrowView.constraints.count == 0) {
  66. self.arrowView.mj_size = self.arrowView.image.size;
  67. self.arrowView.center = arrowCenter;
  68. }
  69. // 圈圈
  70. if (self.loadingView.constraints.count == 0) {
  71. self.loadingView.center = arrowCenter;
  72. }
  73. self.arrowView.tintColor = self.stateLabel.textColor;
  74. }
  75. - (void)setState:(MJRefreshState)state
  76. {
  77. MJRefreshCheckState
  78. // 根据状态做事情
  79. if (state == MJRefreshStateIdle) {
  80. if (oldState == MJRefreshStateRefreshing) {
  81. self.arrowView.transform = CGAffineTransformIdentity;
  82. [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
  83. self.loadingView.alpha = 0.0;
  84. } completion:^(BOOL finished) {
  85. // 如果执行完动画发现不是idle状态,就直接返回,进入其他状态
  86. if (self.state != MJRefreshStateIdle) return;
  87. self.loadingView.alpha = 1.0;
  88. [self.loadingView stopAnimating];
  89. self.arrowView.hidden = NO;
  90. }];
  91. } else {
  92. [self.loadingView stopAnimating];
  93. self.arrowView.hidden = NO;
  94. [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
  95. self.arrowView.transform = CGAffineTransformIdentity;
  96. }];
  97. }
  98. } else if (state == MJRefreshStatePulling) {
  99. [self.loadingView stopAnimating];
  100. self.arrowView.hidden = NO;
  101. [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
  102. self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
  103. }];
  104. } else if (state == MJRefreshStateRefreshing) {
  105. self.loadingView.alpha = 1.0; // 防止refreshing -> idle的动画完毕动作没有被执行
  106. [self.loadingView startAnimating];
  107. self.arrowView.hidden = YES;
  108. }
  109. }
  110. @end