KSGifRefreshFooter.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // KSGifRefreshFooter.m
  3. // StudentDaya
  4. //
  5. // Created by Kyle on 2021/10/15.
  6. // Copyright © 2021 DayaMusic. All rights reserved.
  7. //
  8. #import "KSGifRefreshFooter.h"
  9. #import <Lottie/Lottie.h>
  10. @interface KSGifRefreshFooter ()
  11. @property(nonatomic, strong) LOTAnimationView *loadingView;
  12. @property(nonatomic, strong) NSString *jsonString;
  13. @end
  14. @implementation KSGifRefreshFooter
  15. - (instancetype)init {
  16. if (self = [super init]) {
  17. [self setJsonName:@"teacher_refresh"];
  18. }
  19. return self;
  20. }
  21. - (void)setJsonName:(NSString *)jsonName {
  22. self.jsonString = jsonName;
  23. [self addSubview:self.loadingView];
  24. }
  25. - (LOTAnimationView *)loadingView {
  26. if(_loadingView == nil) {
  27. //1.加载本地json
  28. _loadingView = [LOTAnimationView animationWithFilePath:[[NSBundle mainBundle] pathForResource:self.jsonString ofType:@"json"]];
  29. //2.加载后台给的json(url)
  30. _loadingView.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width / 2.0) - 21, 0, 42, 55);
  31. _loadingView.loopAnimation = YES;
  32. _loadingView.contentMode = UIViewContentModeScaleAspectFill;
  33. _loadingView.animationSpeed = 1.0;
  34. _loadingView.loopAnimation = YES;
  35. }
  36. return _loadingView;
  37. }
  38. #pragma mark - innerMethod
  39. - (void)endRefreshing {
  40. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  41. [super endRefreshing];
  42. });
  43. }
  44. #pragma mark - 监听控件的刷新状态
  45. - (void)setState:(MJRefreshState)state {
  46. MJRefreshCheckState;
  47. if(self.jsonString.length > 0) {
  48. switch (state) {
  49. case MJRefreshStateIdle: //普通闲置状态
  50. {
  51. [self.loadingView stop];
  52. //self.loadingView.hidden = YES;
  53. break;
  54. }
  55. case MJRefreshStatePulling: //松开就可以进行刷新的状态
  56. {
  57. //self.loadingView.hidden = NO;
  58. }
  59. break;
  60. case MJRefreshStateRefreshing: //正在刷新中的状态
  61. {
  62. self.loadingView.animationProgress = 0;
  63. [self.loadingView play];
  64. }
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. }
  71. #pragma mark - 实时监听控件 scrollViewContentOffset
  72. - (void)scrollViewContentOffsetDidChange:(NSDictionary *)change {
  73. [super scrollViewContentOffsetDidChange:change];
  74. if(self.jsonString.length > 0) {
  75. CGPoint point;
  76. id newVelue = [change valueForKey:NSKeyValueChangeNewKey];
  77. [(NSValue *)newVelue getValue:&point];
  78. //id newVelue1 = [change objectForKey:NSKeyValueChangeNewKey];
  79. //CGPoint point1 = ((NSValue *)newVelue1).CGPointValue;//可以取值
  80. //id newVelue2 = [change objectForKey:@"new"];
  81. //CGPoint point2 = *((__bridge CGPoint *)(newVelue2));//无法取到值
  82. self.loadingView.hidden = !(self.pullingPercent);
  83. CGFloat progress = point.y / ([UIScreen mainScreen].bounds.size.height / 3.0);
  84. if(self.state != MJRefreshStateRefreshing) {
  85. self.loadingView.animationProgress = -progress;
  86. }
  87. }
  88. }
  89. - (void)prepare {
  90. [super prepare];
  91. // 设置控件的高度
  92. self.mj_h = 55;
  93. // 初始化文字
  94. [self setTitle:@"" forState:MJRefreshStateIdle];
  95. [self setTitle:@"" forState:MJRefreshStatePulling];
  96. [self setTitle:@"" forState:MJRefreshStateRefreshing];
  97. [self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshBackFooterNoMoreDataText] forState:MJRefreshStateNoMoreData];
  98. }
  99. /*
  100. // Only override drawRect: if you perform custom drawing.
  101. // An empty implementation adversely affects performance during animation.
  102. - (void)drawRect:(CGRect)rect {
  103. // Drawing code
  104. }
  105. */
  106. @end