@@ -42,6 +42,9 @@ @interface YCRefreshManager ()
4242{
4343// scrollView 初始 Inset
4444UIEdgeInsets _originalInset;
45+
46+ // scrollView 开始拖动的 contentOffset
47+ CGPoint _originalOffset;
4548}
4649@property (nonatomic , weak ) UIScrollView *scrollView;
4750@property (nonatomic , weak ) UIPanGestureRecognizer *panGestureRecognizer;
@@ -74,6 +77,7 @@ - (instancetype)initWithScrollView:(UIScrollView *)scrollView {
7477- (void )setupWith : (UIScrollView *)scrollView {
7578// 首次传入时候,将 _originalInset 设置为 scrollView 的 contentInset
7679_originalInset = scrollView.contentInset ;
80+ _originalOffset = scrollView.contentOffset ;
7781
7882// 将 scrollView 和 scrollView 的滑动手势设置为成员变量
7983self.scrollView = scrollView;
@@ -87,7 +91,7 @@ - (void)setupWith:(UIScrollView *)scrollView {
8791}
8892// 监听属性
8993- (void )addObserver {
90- [self .scrollView addObserver: self forKeyPath: @" contentOffset" options: NSKeyValueObservingOptionNew context: nil ];
94+ [self .scrollView addObserver: self forKeyPath: @" contentOffset" options: NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context: nil ];
9195[self .scrollView addObserver: self forKeyPath: @" contentInset" options: NSKeyValueObservingOptionNew context: nil ];
9296[self .scrollView addObserver: self forKeyPath: @" contentSize" options: NSKeyValueObservingOptionNew context: nil ];
9397[self .scrollView addObserver: self forKeyPath: @" frame" options: NSKeyValueObservingOptionNew context: nil ];
@@ -177,7 +181,7 @@ - (void)handleRefreshViewChange:(NSDictionary *)change {
177181- (void )handleContentOffsetChange : (NSDictionary *)change {
178182// scrollview 当前滑动到的位置,去掉 contentInset 的影响
179183CGFloat offsetY = _scrollView.contentOffset .y + _scrollView.contentInset .top ;
180- if (offsetY < 0 ) {
184+ if (offsetY < 0 && _loadmoreState != YCLoadmoreStateNoData && _loadmoreState != YCLoadmoreStateLoading ) {
181185// 如果设置了 refreshAction ,并且现在不处于刷新状态才继续进行
182186if (self.refreshAction && _refreshState != YCRefreshStateRefreshing && _refreshState != YCRefreshStateRefreshed) {
183187
@@ -200,12 +204,13 @@ - (void)handleContentOffsetChange:(NSDictionary *)change {
200204}
201205}
202206
203- if (self.loadmoreAction ) {
207+ if (self.loadmoreAction && _refreshState != YCRefreshStateRefreshing && _refreshState != YCRefreshStateRefreshed ) {
204208CGFloat boundHeight = _scrollView.bounds .size .height ;
205209CGFloat contentHeight = _scrollView.contentSize .height ;
206210offsetY = contentHeight - boundHeight - _scrollView.contentOffset .y + _scrollView.contentInset .bottom ;
207211
208- if (contentHeight > 0 ) {
212+ NSValue *newValue = [change objectForKey: @" new" ];
213+ if (_scrollView.isTracking && contentHeight > 0 && [newValue CGPointValue ].y > _originalOffset.y ) {
209214if (_loadmoreState != YCLoadmoreStateNoData && _loadmoreState != YCLoadmoreStateLoading) {
210215if (offsetY >= [_scrollView.yc_loadmoreView loadmoreHeight ]) {
211216_loadmoreState = YCLoadmoreStatePulling;
@@ -258,10 +263,8 @@ - (void)setScrollContent {
258263}else if (_refreshState == YCRefreshStateRefreshed) {
259264// 重置 scrollView 的 contentInset 到原始数值
260265[self resetScrollViewContentInsetWithCompletion: ^(BOOL finished) {
261- if (finished) {
262- // 说明已经执行过 endRefresh 将 refreshState 重置
263- _refreshState = YCRefreshStateNormal;
264- }
266+ // 说明已经执行过 endRefresh 将 refreshState 重置
267+ _refreshState = YCRefreshStateNormal;
265268}];
266269}
267270}
@@ -271,17 +274,15 @@ - (void)setScrollContent {
271274[self setScrollViewContentInsetForRefreshingAnimated: YES ];
272275}else if (_refreshState == YCRefreshStateRefreshed) {
273276[self resetScrollViewContentInsetWithCompletion: ^(BOOL finished) {
274- if (finished) {
275- // 说明已经执行过 endRefresh 将 refreshState 重置
276- _refreshState = YCRefreshStateNormal;
277- }
277+ // 说明已经执行过 endRefresh 将 refreshState 重置
278+ _refreshState = YCRefreshStateNormal;
278279}];
279280}
280281
281282}
282283// 手势开始触发
283284else if (self.panGestureRecognizer .state == UIGestureRecognizerStateBegan) {
284-
285+ _originalOffset = _scrollView. contentOffset ;
285286}
286287}
287288- (void )tapLoadmoreView {
0 commit comments