iOS 下拉刷新 上拉加载实现原理

时间:2025-01-07 23:34:20

1、下拉刷新 实现原理

if (scrollView.contentOffset.y < -100) {

    [UIView animateWithDuration:1.0 animations:^{

        self.scrollView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);

    } completion:^(BOOL finished) {

        NSLog(@"发起下拉刷新");

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [UIView animateWithDuration:1.0 animations:^{

                self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

            }];

        });

    }];

}

2、上拉加载 实现原理

if (scrollView.bounds.size.height +  scrollView.contentOffset.y >scrollView.contentSize.height) {

    [UIView animateWithDuration:1.0 animations:^{

        self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);

    } completion:^(BOOL finished) {

        NSLog(@"发起上拉加载");

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            [UIView animateWithDuration:1.0 animations:^{

                self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

            }];

        });

    }];

}