Objective-c 下拉刷新,上拉加载实现原理

#pragma mark+++++++++++++++++++下拉刷新原理+++++++++++++++++++网络

  • (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { NSLog(@"scrollViewX:%f scrollViewY:%f",scrollView.contentOffset.x ,scrollView.contentOffset.y); if (scrollView.contentOffset.y <-60) { [UIView animateWithDuration:0.2 animations:^{ self.allTableView.contentInset = UIEdgeInsetsMake(40.0f, 0.0f, 0.0f, 0.0f); } completion:^(BOOL finished){code

    /**
           *  发起网络请求,请求刷新数据
           */
          [_dataSource requestListAllData:^(NSArray *array)
           {
               _allDataSource = [[NSMutableArray alloc]initWithArray:array];
               //刷新
               [_allTableView reloadData];
               self.pag=2;
               [UIView animateWithDuration:0.2 animations:^{
                   self.allTableView.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
               }];
           }];
    
    
      }];

    } } #pragma mark+++++++++++++++++++上拉加载原理+++++++++++++++++++animation

  • (void)scrollViewDidEndDragging:(UIScrollView )scrollView willDecelerate:(BOOL)decelerate { NSLog(@"%f",scrollView.contentOffset.y); NSLog(@"%f",scrollView.frame.size.height); NSLog(@"%f",scrollView.contentSize.height); /*it

    • 关键-->io

    • scrollView一开始并不存在偏移量,可是会设定contentSize的大小,因此contentSize.height永远都会比contentOffset.y高一个手机屏幕的table

    • 高度;上拉加载的效果就是每次滑动到底部时,再往上拉的时候请求更多,那个时候产生的偏移量,就能让contentOffset.y + 手机屏幕尺寸高大于这原理

    • 个滚动视图的contentSize.height */ if (scrollView.contentOffset.y + scrollView.frame.size.height >= scrollView.contentSize.height) { NSLog(@"%d %s",LINE,FUNCTION); [UIView commitAnimations];List

      [UIView animateWithDuration:1.0 animations:^ { //frame发生的偏移量,距离底部往上提升60(可自行设定) self.allTableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); } completion:^(BOOL finished) { __weak typeof(self) weakSelf = self; [_dataSource addListAllDataWithPag:weakSelf.pag Data:^(NSArray *array) { [weakSelf.allDataSource addObjectsFromArray:array]; [weakSelf.allTableView reloadData]; weakSelf.pag+=1;request

      }];

      }];请求

    } }

相关文章
相关标签/搜索