MJRefreshGifHeader.m 3.3 KB

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