MJRefreshStateHeader.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // MJRefreshStateHeader.m
  3. // MJRefreshExample
  4. //
  5. // Created by MJ Lee on 15/4/24.
  6. // Copyright (c) 2015年 小码哥. All rights reserved.
  7. //
  8. #import "MJRefreshStateHeader.h"
  9. @interface MJRefreshStateHeader()
  10. {
  11. /** 显示上一次刷新时间的label */
  12. __unsafe_unretained UILabel *_lastUpdatedTimeLabel;
  13. /** 显示刷新状态的label */
  14. __unsafe_unretained UILabel *_stateLabel;
  15. }
  16. /** 所有状态对应的文字 */
  17. @property (strong, nonatomic) NSMutableDictionary *stateTitles;
  18. @end
  19. @implementation MJRefreshStateHeader
  20. #pragma mark - 懒加载
  21. - (NSMutableDictionary *)stateTitles
  22. {
  23. if (!_stateTitles) {
  24. self.stateTitles = [NSMutableDictionary dictionary];
  25. }
  26. return _stateTitles;
  27. }
  28. - (UILabel *)stateLabel
  29. {
  30. if (!_stateLabel) {
  31. [self addSubview:_stateLabel = [UILabel label]];
  32. }
  33. return _stateLabel;
  34. }
  35. - (UILabel *)lastUpdatedTimeLabel
  36. {
  37. if (!_lastUpdatedTimeLabel) {
  38. [self addSubview:_lastUpdatedTimeLabel = [UILabel label]];
  39. }
  40. return _lastUpdatedTimeLabel;
  41. }
  42. #pragma mark - 公共方法
  43. - (void)setTitle:(NSString *)title forState:(MJRefreshState)state
  44. {
  45. if (title == nil) return;
  46. self.stateTitles[@(state)] = title;
  47. self.stateLabel.text = self.stateTitles[@(self.state)];
  48. }
  49. #pragma mark - 日历获取在9.x之后的系统使用currentCalendar会出异常。在8.0之后使用系统新API。
  50. - (NSCalendar *)currentCalendar {
  51. if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
  52. return [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
  53. }
  54. return [NSCalendar currentCalendar];
  55. }
  56. #pragma mark key的处理
  57. - (void)setLastUpdatedTimeKey:(NSString *)lastUpdatedTimeKey
  58. {
  59. [super setLastUpdatedTimeKey:lastUpdatedTimeKey];
  60. // 如果label隐藏了,就不用再处理
  61. if (self.lastUpdatedTimeLabel.hidden) return;
  62. NSDate *lastUpdatedTime = [[NSUserDefaults standardUserDefaults] objectForKey:lastUpdatedTimeKey];
  63. // 如果有block
  64. if (self.lastUpdatedTimeText) {
  65. self.lastUpdatedTimeLabel.text = self.lastUpdatedTimeText(lastUpdatedTime);
  66. return;
  67. }
  68. if (lastUpdatedTime) {
  69. // 1.获得年月日
  70. NSCalendar *calendar = [self currentCalendar];
  71. NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute;
  72. NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:lastUpdatedTime];
  73. NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
  74. // 2.格式化日期
  75. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  76. if ([cmp1 day] == [cmp2 day]) { // 今天
  77. formatter.dateFormat = @"今天 HH:mm";
  78. } else if ([cmp1 year] == [cmp2 year]) { // 今年
  79. formatter.dateFormat = @"MM-dd HH:mm";
  80. } else {
  81. formatter.dateFormat = @"yyyy-MM-dd HH:mm";
  82. }
  83. NSString *time = [formatter stringFromDate:lastUpdatedTime];
  84. // 3.显示日期
  85. self.lastUpdatedTimeLabel.text = [NSString stringWithFormat:@"最后更新:%@", time];
  86. } else {
  87. self.lastUpdatedTimeLabel.text = @"最后更新:无记录";
  88. }
  89. }
  90. #pragma mark - 覆盖父类的方法
  91. - (void)prepare
  92. {
  93. [super prepare];
  94. // 初始化文字
  95. [self setTitle:MJRefreshHeaderIdleText forState:MJRefreshStateIdle];
  96. [self setTitle:MJRefreshHeaderPullingText forState:MJRefreshStatePulling];
  97. [self setTitle:MJRefreshHeaderRefreshingText forState:MJRefreshStateRefreshing];
  98. }
  99. - (void)placeSubviews
  100. {
  101. [super placeSubviews];
  102. if (self.stateLabel.hidden) return;
  103. BOOL noConstrainsOnStatusLabel = self.stateLabel.constraints.count == 0;
  104. if (self.lastUpdatedTimeLabel.hidden) {
  105. // 状态
  106. if (noConstrainsOnStatusLabel) self.stateLabel.frame = self.bounds;
  107. } else {
  108. CGFloat stateLabelH = self.mj_h * 0.5;
  109. // 状态
  110. if (noConstrainsOnStatusLabel) {
  111. self.stateLabel.mj_x = 0;
  112. self.stateLabel.mj_y = 0;
  113. self.stateLabel.mj_w = self.mj_w;
  114. self.stateLabel.mj_h = stateLabelH;
  115. }
  116. // 更新时间
  117. if (self.lastUpdatedTimeLabel.constraints.count == 0) {
  118. self.lastUpdatedTimeLabel.mj_x = 0;
  119. self.lastUpdatedTimeLabel.mj_y = stateLabelH;
  120. self.lastUpdatedTimeLabel.mj_w = self.mj_w;
  121. self.lastUpdatedTimeLabel.mj_h = self.mj_h - self.lastUpdatedTimeLabel.mj_y;
  122. }
  123. }
  124. }
  125. - (void)setState:(MJRefreshState)state
  126. {
  127. MJRefreshCheckState
  128. // 设置状态文字
  129. self.stateLabel.text = self.stateTitles[@(state)];
  130. // 重新设置key(重新显示时间)
  131. self.lastUpdatedTimeKey = self.lastUpdatedTimeKey;
  132. }
  133. @end