Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions JYRefreshController/Source/JYPullToLoadMoreController.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ - (instancetype)initWithScrollView:(UIScrollView *)scrollView
_autoLoadMore = YES;
_originalContentInsetBottom = scrollView.contentInset.bottom;

[self.scrollView addObserver:self
forKeyPath:@"contentOffset"
options:NSKeyValueObservingOptionNew
context:NULL];
[_scrollView addObserver:self
forKeyPath:@"contentOffset"
options:NSKeyValueObservingOptionNew
context:NULL];
[_scrollView addObserver:self
forKeyPath:@"contentSize"
options:NSKeyValueObservingOptionNew
context:NULL];
[_scrollView addObserver:self
forKeyPath:@"contentInset"
options:NSKeyValueObservingOptionNew
context:NULL];

[self setCustomView:[self defalutRefreshView]];
[self setEnable:YES withAnimation:NO];
Expand All @@ -61,6 +65,7 @@ - (void)dealloc
{
[self.scrollView removeObserver:self forKeyPath:@"contentOffset"];
[self.scrollView removeObserver:self forKeyPath:@"contentSize"];
[self.scrollView removeObserver:self forKeyPath:@"contentInset"];
}

#pragma mark- Property
Expand Down Expand Up @@ -210,9 +215,27 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
{
if ([keyPath isEqualToString:@"contentOffset"]) {
[self checkOffsetsWithChange:change];
} else if ([keyPath isEqualToString:@"contentSize"]) {
}
else if ([keyPath isEqualToString:@"contentSize"]) {
[self layoutLoadMoreView];
}
else if ([keyPath isEqualToString:@"contentInset"]) { // self.scrollView 的 contentInset变化后,需要更新 self.originalContentInsetBottom
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要调用一下 [self layoutLoadMoreView]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看起来不需要主动调用,layoutLoadMoreView 被调用的时候,这里的事情已经做过了。

CGFloat scrollViewInsetBottom = self.scrollView.contentInset.bottom;
CGFloat loadMoreViewHeight = self.loadMoreView.bounds.size.height;

// self.scrollView.contentInset.bottom 只可能存在下面两种情况;
// 如果不满足,说明系统更新了scrollView.contentInset,这时候需要更新 self.originalContentInsetBottom
if (scrollViewInsetBottom != self.originalContentInsetBottom
&& scrollViewInsetBottom != self.originalContentInsetBottom + loadMoreViewHeight) {

if (scrollViewInsetBottom > self.originalContentInsetBottom + loadMoreViewHeight) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该是 != 会更好一点吧?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实这样最好,加上了状态的判断。
用 > 是因为最后的操作是 减去,考虑到系统加上的inset.bottom不会是负的。

if (scrollViewInsetBottom != self.originalContentInsetBottom
        && scrollViewInsetBottom != self.originalContentInsetBottom + loadMoreViewHeight) {

      if (_enable && _autoLoadMore && scrollViewInsetBottom > self.originalContentInsetBottom + loadMoreViewHeight) {
        self.originalContentInsetBottom = scrollViewInsetBottom - self.originalContentInsetBottom -loadMoreViewHeight;
      }
      else if (scrollViewInsetBottom > self.originalContentInsetBottom) {
        self.originalContentInsetBottom = scrollViewInsetBottom - self.originalContentInsetBottom;
      }
    }
  }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果要支持可设置 contentinset
有一种可能是 先变大后变小了 或者有可能为复数. 都囊括进去吧

self.originalContentInsetBottom = scrollViewInsetBottom - self.originalContentInsetBottom -loadMoreViewHeight;
}
else if (scrollViewInsetBottom > self.originalContentInsetBottom) {
self.originalContentInsetBottom = scrollViewInsetBottom;
}
}
}
}

#pragma mark - Private Methods
Expand Down