MJRefreshBackNormalFooter.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. @interface MJRefreshBackNormalFooter()
  10. {
  11. __unsafe_unretained UIImageView *_arrowView;
  12. }
  13. @property (weak, nonatomic) UIActivityIndicatorView *loadingView;
  14. @end
  15. @implementation MJRefreshBackNormalFooter
  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. - (void)setActivityIndicatorViewStyle:(UIActivityIndicatorViewStyle)activityIndicatorViewStyle
  36. {
  37. _activityIndicatorViewStyle = activityIndicatorViewStyle;
  38. self.loadingView = nil;
  39. [self setNeedsLayout];
  40. }
  41. #pragma makr - 重写父类的方法
  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 -= 100;
  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. }
  67. - (void)setState:(MJRefreshState)state
  68. {
  69. MJRefreshCheckState
  70. // 根据状态做事情
  71. if (state == MJRefreshStateIdle) {
  72. if (oldState == MJRefreshStateRefreshing) {
  73. self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
  74. [UIView animateWithDuration:MJRefreshSlowAnimationDuration animations:^{
  75. self.loadingView.alpha = 0.0;
  76. } completion:^(BOOL finished) {
  77. self.loadingView.alpha = 1.0;
  78. [self.loadingView stopAnimating];
  79. self.arrowView.hidden = NO;
  80. }];
  81. } else {
  82. self.arrowView.hidden = NO;
  83. [self.loadingView stopAnimating];
  84. [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
  85. self.arrowView.transform = CGAffineTransformMakeRotation(0.000001 - M_PI);
  86. }];
  87. }
  88. } else if (state == MJRefreshStatePulling) {
  89. self.arrowView.hidden = NO;
  90. [self.loadingView stopAnimating];
  91. [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
  92. self.arrowView.transform = CGAffineTransformIdentity;
  93. }];
  94. } else if (state == MJRefreshStateRefreshing) {
  95. self.arrowView.hidden = YES;
  96. [self.loadingView startAnimating];
  97. } else if (state == MJRefreshStateNoMoreData) {
  98. self.arrowView.hidden = YES;
  99. [self.loadingView stopAnimating];
  100. }
  101. }
  102. @end