MJRefreshBackGifFooter.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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)prepare
  60. {
  61. [super prepare];
  62. // 初始化间距
  63. self.labelLeftInset = 20;
  64. }
  65. - (void)setPullingPercent:(CGFloat)pullingPercent
  66. {
  67. [super setPullingPercent:pullingPercent];
  68. NSArray *images = self.stateImages[@(MJRefreshStateIdle)];
  69. if (self.state != MJRefreshStateIdle || images.count == 0) return;
  70. [self.gifView stopAnimating];
  71. NSUInteger index = images.count * pullingPercent;
  72. if (index >= images.count) index = images.count - 1;
  73. self.gifView.image = images[index];
  74. }
  75. - (void)placeSubviews
  76. {
  77. [super placeSubviews];
  78. if (self.gifView.constraints.count) return;
  79. self.gifView.frame = self.bounds;
  80. if (self.stateLabel.hidden) {
  81. self.gifView.contentMode = UIViewContentModeCenter;
  82. } else {
  83. self.gifView.contentMode = UIViewContentModeRight;
  84. self.gifView.mj_w = self.mj_w * 0.5 - self.labelLeftInset - self.stateLabel.mj_textWith * 0.5;
  85. }
  86. }
  87. - (void)setState:(MJRefreshState)state
  88. {
  89. MJRefreshCheckState
  90. // 根据状态做事情
  91. if (state == MJRefreshStatePulling || state == MJRefreshStateRefreshing) {
  92. NSArray *images = self.stateImages[@(state)];
  93. if (images.count == 0) return;
  94. self.gifView.hidden = NO;
  95. [self.gifView stopAnimating];
  96. if (images.count == 1) { // 单张图片
  97. self.gifView.image = [images lastObject];
  98. } else { // 多张图片
  99. self.gifView.animationImages = images;
  100. self.gifView.animationDuration = [self.stateDurations[@(state)] doubleValue];
  101. [self.gifView startAnimating];
  102. }
  103. } else if (state == MJRefreshStateIdle) {
  104. self.gifView.hidden = NO;
  105. } else if (state == MJRefreshStateNoMoreData) {
  106. self.gifView.hidden = YES;
  107. }
  108. }
  109. @end