MJRefreshAutoGifFooter.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // MJRefreshAutoGifFooter.m
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 15/4/24.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJRefreshAutoGifFooter.h"
  9. @interface MJRefreshAutoGifFooter()
  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 MJRefreshAutoGifFooter
  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)placeSubviews
  66. {
  67. [super placeSubviews];
  68. if (self.gifView.constraints.count) return;
  69. self.gifView.frame = self.bounds;
  70. if (self.isRefreshingTitleHidden) {
  71. self.gifView.contentMode = UIViewContentModeCenter;
  72. } else {
  73. self.gifView.contentMode = UIViewContentModeRight;
  74. self.gifView.mj_w = self.mj_w * 0.5 - self.labelLeftInset - self.stateLabel.mj_textWith * 0.5;
  75. }
  76. }
  77. - (void)setState:(MJRefreshState)state
  78. {
  79. MJRefreshCheckState
  80. // 根据状态做事情
  81. if (state == MJRefreshStateRefreshing) {
  82. NSArray *images = self.stateImages[@(state)];
  83. if (images.count == 0) return;
  84. [self.gifView stopAnimating];
  85. self.gifView.hidden = NO;
  86. if (images.count == 1) { // 单张图片
  87. self.gifView.image = [images lastObject];
  88. } else { // 多张图片
  89. self.gifView.animationImages = images;
  90. self.gifView.animationDuration = [self.stateDurations[@(state)] doubleValue];
  91. [self.gifView startAnimating];
  92. }
  93. } else if (state == MJRefreshStateNoMoreData || state == MJRefreshStateIdle) {
  94. [self.gifView stopAnimating];
  95. self.gifView.hidden = YES;
  96. }
  97. }
  98. @end