2
0

SDRefreshHeaderView.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // SDRefreshHeaderView.m
  3. // SDRefreshView
  4. //
  5. // Created by aier on 15-2-22.
  6. // Copyright (c) 2015年 GSD. All rights reserved.
  7. //
  8. /**
  9. *******************************************************
  10. * *
  11. * 感谢您的支持, 如果下载的代码在使用过程中出现BUG或者其他问题 *
  12. * 您可以发邮件到gsdios@126.com 或者 到 *
  13. * https://github.com/gsdios?tab=repositories 提交问题 *
  14. * *
  15. *******************************************************
  16. */
  17. #import "SDRefreshHeaderView.h"
  18. #import "UIView+SDExtension.h"
  19. @implementation SDRefreshHeaderView
  20. {
  21. BOOL _hasLayoutedForManuallyRefreshing;
  22. }
  23. - (id)initWithFrame:(CGRect)frame
  24. {
  25. self = [super initWithFrame:frame];
  26. if (self) {
  27. self.textForNormalState = @"下拉可以加载最新数据";
  28. self.stateIndicatorViewNormalTransformAngle = 0;
  29. self.stateIndicatorViewWillRefreshStateTransformAngle = M_PI;
  30. [self setRefreshState:SDRefreshViewStateNormal];
  31. }
  32. return self;
  33. }
  34. - (CGFloat)yOfCenterPoint
  35. {
  36. // if (self.isManuallyRefreshing && self.isEffectedByNavigationController && SDRefreshViewMethodIOS7) {
  37. // return - (self.sd_height * 0.5 + self.originalEdgeInsets.top - SDKNavigationBarHeight);
  38. // }
  39. return - (self.sd_height * 0.5);
  40. }
  41. - (void)didMoveToSuperview
  42. {
  43. [super didMoveToSuperview];
  44. self.scrollViewEdgeInsets = UIEdgeInsetsMake(self.frame.size.height, 0, 0, 0);
  45. }
  46. - (void)layoutSubviews
  47. {
  48. [super layoutSubviews];
  49. self.center = CGPointMake(self.scrollView.sd_width * 0.5, [self yOfCenterPoint]);
  50. // 手动刷新
  51. if (self.isManuallyRefreshing && !_hasLayoutedForManuallyRefreshing && self.scrollView.contentInset.top > 0) {
  52. self.activityIndicatorView.hidden = NO;
  53. // 模拟下拉操作
  54. CGPoint temp = self.scrollView.contentOffset;
  55. temp.y -= self.sd_height * 2;
  56. self.scrollView.contentOffset = temp; // 触发准备刷新
  57. temp.y += self.sd_height;
  58. self.scrollView.contentOffset = temp; // 触发刷新
  59. _hasLayoutedForManuallyRefreshing = YES;
  60. } else {
  61. self.activityIndicatorView.hidden = !self.isManuallyRefreshing;
  62. }
  63. }
  64. - (void)autoRefreshWhenViewDidAppear
  65. {
  66. self.isManuallyRefreshing = YES;
  67. }
  68. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  69. {
  70. if (![keyPath isEqualToString:SDRefreshViewObservingkeyPath] || self.refreshState == SDRefreshViewStateRefreshing) return;
  71. CGFloat y = [change[@"new"] CGPointValue].y;
  72. CGFloat criticalY = -self.sd_height - self.scrollView.contentInset.top;
  73. // 只有在 y<=0 以及 scrollview的高度不为0 时才判断
  74. if ((y > 0) || (self.scrollView.bounds.size.height == 0)) return;
  75. // 触发SDRefreshViewStateRefreshing状态
  76. if (y <= criticalY && (self.refreshState == SDRefreshViewStateWillRefresh) && !self.scrollView.isDragging) {
  77. [self setRefreshState:SDRefreshViewStateRefreshing];
  78. return;
  79. }
  80. // 触发SDRefreshViewStateWillRefresh状态
  81. if (y < criticalY && (SDRefreshViewStateNormal == self.refreshState)) {
  82. [self setRefreshState:SDRefreshViewStateWillRefresh];
  83. return;
  84. }
  85. if (y > criticalY && self.scrollView.isDragging && (SDRefreshViewStateNormal != self.refreshState)) {
  86. [self setRefreshState:SDRefreshViewStateNormal];
  87. }
  88. }
  89. @end