【Swift/Objective-c】公司项目优化(二) - 下拉刷新抖动问题

  使用MJRefresh进行列表下拉刷新时,会出现列表上下颤抖问题async

  抖动的缘由spa

  咱们先来看看在手松开以后咱们对scrollView作了什么事情:code

  ScrollViewDidEndDragging => setContentInset:blog

  为了保证在“Loading”的状态下,下拉刷新控件能够展现,咱们对contentInset作了修改,增长了inset的top. 那这样一步操做为何会致使scrollView抖动一下呢。get

  我在scrollViewDidScroll:中打了个断点,来看看在setContentInset:以后发生了什么事情。 我设置的inset.top = 64; 结果发现scrollView的contentOffset发生了这样的变化:(0, -64) =>  (0, -133) => (0, -64)animation

  由以上数据能够看出,contentOffset在这个过程当中先被向下移动了一段,再回归正常。 猜想问题缘由:源码

下拉松开以后, scrollView自己的 bounce 效果 与 当前设置inset冲突了

  因为我设置的it

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

  设置了以后就出现这个问题。若是不设置这句话就没有这个问题,可是跟他们UI给的效果图就不同了。io

  • 看了一下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];
                }];
            })

    因而我尝试修改代码,改为以下:ast

    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];
                        }];
                     });

    二、给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) {
            }];
        });
相关文章
相关标签/搜索