MJRefreshAutoStateFooter.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // MJRefreshAutoStateFooter.m
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 15/6/13.
  6. // Copyright © 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJRefreshAutoStateFooter.h"
  9. @interface MJRefreshAutoStateFooter()
  10. {
  11. /** 显示刷新状态的label */
  12. __unsafe_unretained UILabel *_stateLabel;
  13. }
  14. /** 所有状态对应的文字 */
  15. @property (strong, nonatomic) NSMutableDictionary *stateTitles;
  16. @end
  17. @implementation MJRefreshAutoStateFooter
  18. #pragma mark - 懒加载
  19. - (NSMutableDictionary *)stateTitles
  20. {
  21. if (!_stateTitles) {
  22. self.stateTitles = [NSMutableDictionary dictionary];
  23. }
  24. return _stateTitles;
  25. }
  26. - (UILabel *)stateLabel
  27. {
  28. if (!_stateLabel) {
  29. [self addSubview:_stateLabel = [UILabel mj_label]];
  30. }
  31. return _stateLabel;
  32. }
  33. #pragma mark - 公共方法
  34. - (void)setTitle:(NSString *)title forState:(MJRefreshState)state
  35. {
  36. if (title == nil) return;
  37. self.stateTitles[@(state)] = title;
  38. self.stateLabel.text = self.stateTitles[@(self.state)];
  39. }
  40. #pragma mark - 私有方法
  41. - (void)stateLabelClick
  42. {
  43. if (self.state == MJRefreshStateIdle) {
  44. [self beginRefreshing];
  45. }
  46. }
  47. #pragma mark - 重写父类的方法
  48. - (void)prepare
  49. {
  50. [super prepare];
  51. // 初始化间距
  52. self.labelLeftInset = MJRefreshLabelLeftInset;
  53. // 初始化文字
  54. [self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshAutoFooterIdleText] forState:MJRefreshStateIdle];
  55. [self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshAutoFooterRefreshingText] forState:MJRefreshStateRefreshing];
  56. [self setTitle:[NSBundle mj_localizedStringForKey:MJRefreshAutoFooterNoMoreDataText] forState:MJRefreshStateNoMoreData];
  57. // 监听label
  58. self.stateLabel.userInteractionEnabled = YES;
  59. [self.stateLabel addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(stateLabelClick)]];
  60. }
  61. - (void)placeSubviews
  62. {
  63. [super placeSubviews];
  64. if (self.stateLabel.constraints.count) return;
  65. // 状态标签
  66. self.stateLabel.frame = self.bounds;
  67. }
  68. - (void)setState:(MJRefreshState)state
  69. {
  70. MJRefreshCheckState
  71. if (self.isRefreshingTitleHidden && state == MJRefreshStateRefreshing) {
  72. self.stateLabel.text = nil;
  73. } else {
  74. self.stateLabel.text = self.stateTitles[@(state)];
  75. }
  76. }
  77. @end