2
0

SDRefreshFooterView.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // SDRefreshFooterView.m
  3. // SDRefreshView
  4. //
  5. // Created by aier on 15-2-23.
  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 "SDRefreshFooterView.h"
  18. #import "UIView+SDExtension.h"
  19. @implementation SDRefreshFooterView
  20. {
  21. CGFloat _originalScrollViewContentHeight;
  22. }
  23. - (id)initWithFrame:(CGRect)frame
  24. {
  25. self = [super initWithFrame:frame];
  26. if (self) {
  27. self.textForNormalState = @"上拉可以加载最新数据";
  28. self.stateIndicatorViewNormalTransformAngle = M_PI;
  29. self.stateIndicatorViewWillRefreshStateTransformAngle = 0;
  30. [self setRefreshState:SDRefreshViewStateNormal];
  31. }
  32. return self;
  33. }
  34. - (void)layoutSubviews
  35. {
  36. [super layoutSubviews];
  37. self.activityIndicatorView.hidden = YES;
  38. _originalScrollViewContentHeight = self.scrollView.contentSize.height;
  39. self.center = CGPointMake(self.scrollView.sd_width * 0.5, self.scrollView.contentSize.height + self.sd_height * 0.5); // + self.scrollView.contentInset.bottom
  40. self.hidden = [self shouldHide];
  41. }
  42. - (void)didMoveToSuperview
  43. {
  44. [super didMoveToSuperview];
  45. self.scrollViewEdgeInsets = UIEdgeInsetsMake(0, 0, self.sd_height, 0);
  46. }
  47. - (BOOL)shouldHide
  48. {
  49. if (self.isEffectedByNavigationController) {
  50. return (self.scrollView.bounds.size.height - SDKNavigationBarHeight > self.sd_y); // + self.scrollView.contentInset.bottom
  51. }
  52. return (self.scrollView.bounds.size.height> self.sd_y); // + self.scrollView.contentInset.bottom
  53. }
  54. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  55. {
  56. if (![keyPath isEqualToString:SDRefreshViewObservingkeyPath] || self.refreshState == SDRefreshViewStateRefreshing) return;
  57. CGFloat y = [change[@"new"] CGPointValue].y;
  58. CGFloat criticalY = self.scrollView.contentSize.height - self.scrollView.sd_height + self.sd_height + self.scrollView.contentInset.bottom;
  59. // 如果scrollView内容有增减,重新调整refreshFooter位置
  60. if (self.scrollView.contentSize.height != _originalScrollViewContentHeight) {
  61. [self layoutSubviews];
  62. }
  63. // 只有在 y>0 以及 scrollview的高度不为0 时才判断
  64. if ((y <= 0) || (self.scrollView.bounds.size.height == 0)) return;
  65. // 触发SDRefreshViewStateRefreshing状态
  66. if (y <= criticalY && (self.refreshState == SDRefreshViewStateWillRefresh) && !self.scrollView.isDragging) {
  67. [self setRefreshState:SDRefreshViewStateRefreshing];
  68. return;
  69. }
  70. // 触发SDRefreshViewStateWillRefresh状态
  71. if (y > criticalY && (SDRefreshViewStateNormal == self.refreshState)) {
  72. if (self.hidden) return;
  73. [self setRefreshState:SDRefreshViewStateWillRefresh];
  74. return;
  75. }
  76. if (y <= criticalY && self.scrollView.isDragging && (SDRefreshViewStateNormal != self.refreshState)) {
  77. [self setRefreshState:SDRefreshViewStateNormal];
  78. }
  79. }
  80. @end