KSGifRefreshHeader.m 3.4 KB

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