MJRefreshNormalHeader.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. @interface MJRefreshNormalHeader()
  10. {
  11. __unsafe_unretained UIImageView *_arrowView;
  12. }
  13. @property (weak, nonatomic) UIActivityIndicatorView *loadingView;
  14. @end
  15. @implementation MJRefreshNormalHeader
  16. #pragma mark - 懒加载子控件
  17. - (UIImageView *)arrowView
  18. {
  19. if (!_arrowView) {
  20. UIImage *image = [UIImage imageNamed:MJRefreshSrcName(@"arrow.png")] ?: [UIImage imageNamed:MJRefreshFrameworkSrcName(@"arrow.png")];
  21. UIImageView *arrowView = [[UIImageView alloc] initWithImage:image];
  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 makr - 重写父类的方法
  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. arrowCenterX -= 100;
  55. }
  56. CGFloat arrowCenterY = self.mj_h * 0.5;
  57. CGPoint arrowCenter = CGPointMake(arrowCenterX, arrowCenterY);
  58. // 箭头
  59. if (self.arrowView.constraints.count == 0) {
  60. self.arrowView.mj_size = self.arrowView.image.size;
  61. self.arrowView.center = arrowCenter;
  62. }
  63. // 圈圈
  64. if (self.loadingView.constraints.count == 0) {
  65. self.loadingView.center = arrowCenter;
  66. }
  67. }
  68. - (void)setState:(MJRefreshState)state
  69. {
  70. MJRefreshCheckState
  71. // 根据状态做事情
  72. if (state == MJRefreshStateIdle) {
  73. if (oldState == MJRefreshStateRefreshing) {
  74. self.arrowView.transform = CGAffineTransformIdentity;
  75. [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
  76. self.loadingView.alpha = 0.0;
  77. } completion:^(BOOL finished) {
  78. // 如果执行完动画发现不是idle状态,就直接返回,进入其他状态
  79. if (self.state != MJRefreshStateIdle) return;
  80. self.loadingView.alpha = 1.0;
  81. [self.loadingView stopAnimating];
  82. self.arrowView.hidden = NO;
  83. }];
  84. } else {
  85. [self.loadingView stopAnimating];
  86. self.arrowView.hidden = NO;
  87. [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
  88. self.arrowView.transform = CGAffineTransformIdentity;
  89. }];
  90. }
  91. } else if (state == MJRefreshStatePulling) {
  92. [self.loadingView stopAnimating];
  93. self.arrowView.hidden = NO;
  94. [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
  95. self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
  96. }];
  97. } else if (state == MJRefreshStateRefreshing) {
  98. self.loadingView.alpha = 1.0; // 防止refreshing -> idle的动画完毕动作没有被执行
  99. [self.loadingView startAnimating];
  100. self.arrowView.hidden = YES;
  101. }
  102. }
  103. @end