首先为UIScrollView添加加载提示View
UILabel *refresh = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, 320, 50)];
refresh.text = @"下拉刷新";
self.refresh = refresh;
refresh.tag = 0;
[scrollV addSubview:refresh];
实现UIScrollViewDelegate
//offset发生改变
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.y <= -50) {
if (self.refresh.tag == 0) {
self.refresh.text = @"松开刷新";
}
self.refresh.tag = 1;
}else{
//防止用户在下拉到contentOffset.y <= -50后不松手,然后又往回滑动,需要将值设为默认状态
self.refresh.tag = 0;
self.refresh.text = @"下拉刷新";
}
}
//即将结束拖拽
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint*)targetContentOffset{
if (self.refresh.tag == 1) {
[UIView animateWithDuration:.3 animations:^{
self.refresh.text = @"加载中";
scrollView.contentInset = UIEdgeInsetsMake(50.0f, 0.0f, 0.0f, 0.0f);
}];
//数据加载成功后执行;这里为了模拟加载效果,一秒后执行恢复原状代码
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:.3 animations:^{
self.refresh.tag = 0;
self.refresh.text = @"下拉刷新";
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}];
});
}
}