美文网首页
自定义刷新

自定义刷新

作者: 守护地中海的花 | 来源:发表于2019-08-29 14:39 被阅读0次
    QQ20200601-161232-HD.gif

    属性

    //刷新模块
    @property(nonatomic,strong)LOTAnimationView *lottieView;
    @property(nonatomic,assign)MJRefreshState refreshState;
    

    初始化

    self.refreshState = MJRefreshStateIdle;
    

    懒加载

    - (LOTAnimationView *)lottieView
    {
        if (!_lottieView) {
            LOTAnimationView *view = [LOTAnimationView animationNamed:@"222-trail-loading"];
            view.loopAnimation = YES;
            view.contentMode = UIViewContentModeScaleAspectFit;
            _lottieView = view;
        }
        return _lottieView;
    }
    //放到头部 所以这里自由发挥一下吧
    [tableHeaderView addSubview:self.lottieView];
    [self.lottieView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(100*ADAPTER_WIDTH);
    }];
    

    ScrollViewDidScroll方法

    CGFloat offset = scrollView.contentOffset.y + scrollView.contentInset.top;
    //刷新
    /**
     普通闲置状态
     MJRefreshStateIdle = 1,
     松开就可以进行刷新的状态
     MJRefreshStatePulling,
     正在刷新中的状态
     MJRefreshStateRefreshing,
     即将刷新的状态
     MJRefreshStateWillRefresh,
     所有数据加载完毕,没有更多的数据了
     MJRefreshStateNoMoreData
    
     1.isDragging = YES 是拖拽的时候 松手isDragging = NO
     */
    if (offset < 0) {
        CGFloat pullingOffset = kStatusBarHeight;
        CGFloat offsetY = fabs(offset);
        if (scrollView.isDragging) {
            //拖拽
            if (offsetY >= pullingOffset) {
                //->刷新的状态
                if (self.refreshState == MJRefreshStateIdle) {
                    self.refreshState = MJRefreshStatePulling;
                }
                //刷新状态-刷新动画
                if (self.refreshState == MJRefreshStatePulling) {
                    CGFloat moreY = offsetY - pullingOffset;
                    CGFloat percent = (moreY / 50*ADAPTER_WIDTH);
                    percent = percent - floor(percent);
                    percent = MIN(percent, 1);
                    if (percent > 0.1) {
                        self.lottieView.hidden = NO;
                    } else {
                        self.lottieView.hidden = YES;
                    }
                    self.lottieView.animationProgress = percent;
                }
            } else {
                //恢复默认状态
                if (self.refreshState == MJRefreshStatePulling) {
                    self.refreshState = MJRefreshStateIdle;
                    self.lottieView.hidden = YES;
                    self.lottieView.animationProgress = 0;
                }
            }
        } else {
            //松手
            if (self.refreshState == MJRefreshStatePulling) {
                self.refreshState = MJRefreshStateRefreshing;
                self.lottieView.hidden = NO;
                [self.lottieView play];
                [self loadNewData];
                //添加一个setContentOffset
                CGFloat refreshContentOffsetY = kStatusBarHeight;
                [scrollView setContentOffset:CGPointMake(0, - (refreshContentOffsetY)) animated:YES];
            }
        }
    } else {
        //动画进行时 下滑解决
        if (offset > 50*ADAPTER_WIDTH) {
            self.refreshState = MJRefreshStateIdle;
            self.lottieView.hidden = YES;
            self.lottieView.animationProgress = 0;
            [self.lottieView pause];
        }
    }
    
    

    停止刷新

    - (void)endRefresh
    {
        self.refreshState = MJRefreshStateIdle;
        self.lottieView.hidden = YES;
        self.lottieView.animationProgress = 0;
        [self.lottieView pause];
        //只有刷新后偏移量和设定一直才恢复偏移量为0
        CGFloat refreshContentOffsetY = kStatusBarHeight;
        if (fabs(self.mainView.contentOffset.y) == refreshContentOffsetY) {
            [self.mainView setContentOffset:CGPointMake(0, - (0*ADAPTER_WIDTH)) animated:YES];
        }
    }
    

    相关文章

      网友评论

          本文标题:自定义刷新

          本文链接:https://www.haomeiwen.com/subject/ngeiectx.html