MJRefreshBackGifFooter.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // MJRefreshBackGifFooter.m
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 15/4/24.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJRefreshBackGifFooter.h"
  9. @interface MJRefreshBackGifFooter()
  10. {
  11. __unsafe_unretained UIImageView *_gifView;
  12. }
  13. /** 所有状态对应的动画图片 */
  14. @property (strong, nonatomic) NSMutableDictionary *stateImages;
  15. /** 所有状态对应的动画时间 */
  16. @property (strong, nonatomic) NSMutableDictionary *stateDurations;
  17. @end
  18. @implementation MJRefreshBackGifFooter
  19. #pragma mark - 懒加载
  20. - (UIImageView *)gifView
  21. {
  22. if (!_gifView) {
  23. UIImageView *gifView = [[UIImageView alloc] init];
  24. [self addSubview:_gifView = gifView];
  25. }
  26. return _gifView;
  27. }
  28. - (NSMutableDictionary *)stateImages
  29. {
  30. if (!_stateImages) {
  31. self.stateImages = [NSMutableDictionary dictionary];
  32. }
  33. return _stateImages;
  34. }
  35. - (NSMutableDictionary *)stateDurations
  36. {
  37. if (!_stateDurations) {
  38. self.stateDurations = [NSMutableDictionary dictionary];
  39. }
  40. return _stateDurations;
  41. }
  42. #pragma mark - 公共方法
  43. - (void)setImages:(NSArray *)images duration:(NSTimeInterval)duration forState:(MJRefreshState)state
  44. {
  45. if (images == nil) return;
  46. self.stateImages[@(state)] = images;
  47. self.stateDurations[@(state)] = @(duration);
  48. /* 根据图片设置控件的高度 */
  49. UIImage *image = [images firstObject];
  50. if (image.size.height > self.mj_h) {
  51. self.mj_h = image.size.height;
  52. }
  53. }
  54. - (void)setImages:(NSArray *)images forState:(MJRefreshState)state
  55. {
  56. [self setImages:images duration:images.count * 0.1 forState:state];
  57. }
  58. #pragma mark - 实现父类的方法
  59. - (void)setPullingPercent:(CGFloat)pullingPercent
  60. {
  61. [super setPullingPercent:pullingPercent];
  62. NSArray *images = self.stateImages[@(MJRefreshStateIdle)];
  63. if (self.state != MJRefreshStateIdle || images.count == 0) return;
  64. [self.gifView stopAnimating];
  65. NSUInteger index = images.count * pullingPercent;
  66. if (index >= images.count) index = images.count - 1;
  67. self.gifView.image = images[index];
  68. }
  69. - (void)placeSubviews
  70. {
  71. [super placeSubviews];
  72. if (self.gifView.constraints.count) return;
  73. self.gifView.frame = self.bounds;
  74. if (self.stateLabel.hidden) {
  75. self.gifView.contentMode = UIViewContentModeCenter;
  76. } else {
  77. self.gifView.contentMode = UIViewContentModeRight;
  78. self.gifView.mj_w = self.mj_w * 0.5 - 90;
  79. }
  80. }
  81. - (void)setState:(MJRefreshState)state
  82. {
  83. MJRefreshCheckState
  84. // 根据状态做事情
  85. if (state == MJRefreshStatePulling || state == MJRefreshStateRefreshing) {
  86. NSArray *images = self.stateImages[@(state)];
  87. if (images.count == 0) return;
  88. self.gifView.hidden = NO;
  89. [self.gifView stopAnimating];
  90. if (images.count == 1) { // 单张图片
  91. self.gifView.image = [images lastObject];
  92. } else { // 多张图片
  93. self.gifView.animationImages = images;
  94. self.gifView.animationDuration = [self.stateDurations[@(state)] doubleValue];
  95. [self.gifView startAnimating];
  96. }
  97. } else if (state == MJRefreshStateIdle) {
  98. self.gifView.hidden = NO;
  99. } else if (state == MJRefreshStateNoMoreData) {
  100. self.gifView.hidden = YES;
  101. }
  102. }
  103. @end