動態

詳情 返回 返回

【Swift/Objective-c】公司項目優化(二) - 下拉刷新抖動問題 - 動態 詳情

  使用MJRefresh進行列表下拉刷新時,會出現列表上下顫抖問題

  抖動的原因

  我們先來看看在手鬆開之後我們對scrollView做了什麼事情:

  ScrollViewDidEndDragging => setContentInset:

  為了保證在“Loading”的狀態下,下拉刷新控件可以展示,我們對contentInset做了修改,增加了inset的top. 那這樣一步操作為什麼會導致scrollView抖動一下呢。

  我在scrollViewDidScroll:中打了個斷點,來看看在setContentInset:之後發生了什麼事情。 我設置的inset.top = 64; 結果發現scrollView的contentOffset發生了這樣的變化:(0, -64) =>  (0, -133) => (0, -64)

  由以上數據可以看出,contentOffset在這個過程中先被向下移動了一段,再回歸正常。 猜測問題原因:

下拉鬆開之後, scrollView本身的 bounce 效果 與 當前設置inset衝突了

  由於我設置的

mTableView.contentInset = UIEdgeInsets(top: kTopNavigationSafeMargin, left: 0, bottom: kTabBarHeight, right: 0)

  設置了之後就出現這個問題。如果不設置這句話就沒有這個問題,但是跟他們UI給的效果圖就不一樣了。

  • 看了一下MJRefresh的源碼:
            MJRefreshDispatchAsyncOnMainQueue({
                [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
                    if (self.scrollView.panGestureRecognizer.state != UIGestureRecognizerStateCancelled) {
                        CGFloat top = self.scrollViewOriginalInset.top + self.mj_h;
                        // 增加滾動區域top
                        self.scrollView.mj_insetT = top;
                        // 設置滾動位置
                        CGPoint offset = self.scrollView.contentOffset;
                        offset.y = -top;
                        [self.scrollView setContentOffset:offset animated:NO];
                    }
                } completion:^(BOOL finished) {
                    [self executeRefreshingCallback];
                }];
            })

    於是我嘗試修改代碼,改成如下:

    dispatch_async(dispatch_get_main_queue(), ^{
                        [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{
                            CGFloat top = self.scrollViewOriginalInset.top + self.mj_h;
                            // 增加滾動區域top
                            self.scrollView.mj_insetT = top;
                            // 判斷了一下 這裏面
                            if ([self.scrollView isKindOfClass:[UICollectionView class]]) {
                                self.scrollView.mj_offsetY = - top;
                            }else {
                                [self.scrollView setContentOffset:CGPointMake(0, -top) animated:NO];
                            }
                        } completion:^(BOOL finished) {
                            [self executeRefreshingCallback];
                        }];
                     });

    2、給mTableVeiw的cell一個預估高度estimatedRowHeight;

    解決了。

       其他大神解決方法:

dispatch_async(dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:kAnimationDuration animations:^{
                self.scrollView.contentInset = inset;
                [self.scrollView setContentOffset:CGPointMake(0, -inset.top) animated:NO];
            } completion:^(BOOL finished) {
            }];
        });

 

user avatar sishuiliunian_58f891c129ab1 頭像 segfal_coder 頭像 lnxlee 頭像
點贊 3 用戶, 點贊了這篇動態!
點贊

Add a new 評論

Some HTML is okay.