Iphone ScrollView中的滚动量将开始滚动
有没有办法找到UITableView在任何方向上的滚动量?我感兴趣的是数量,而不是方向 您可以使用以下语法找到它Iphone ScrollView中的滚动量将开始滚动,iphone,ios,uitableview,uiscrollviewdelegate,Iphone,Ios,Uitableview,Uiscrollviewdelegate,有没有办法找到UITableView在任何方向上的滚动量?我感兴趣的是数量,而不是方向 您可以使用以下语法找到它 NSLog(@"scrolled:%f",yourtableview.contentOffset.y);//y for vertical, x for horizontal 当拖动开始和结束时,需要测量UITableView的contentOffset。取两者之间的差值,它将给出从初始位置到最终位置的变化量 CGPoint oldOffset; CGPoint newOffset;
NSLog(@"scrolled:%f",yourtableview.contentOffset.y);//y for vertical, x for horizontal
当拖动开始和结束时,需要测量
UITableView
的contentOffset
。取两者之间的差值,它将给出从初始位置到最终位置的变化量
CGPoint oldOffset;
CGPoint newOffset;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
oldOffset = scrollView.contentOffset;
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
newOffset = *targetContentOffset;
CGPoint diff = {newOffset.x - oldOffset.x, newOffset.y - oldOffset.y};
// Where diff.x => amount of change in x-coord of offset
// diff.y => amount of change in y coord of offset
}
希望有帮助 通过查看表视图的contentOffset属性,可以轻松获取表视图的确切偏移量。对于垂直滚动,请查看:
tableView.contentOffset.y;
有了它,您可以将tableview带到任何特定位置
[theTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:savedScrollPosition inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
CGPoint point = theTableView.contentOffset;
point .y -= theTableView.rowHeight;
theTableView.contentOffset = point;
对于10后加载更多传感器的要求,可以使用此逻辑
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
CGPoint offset = aScrollView.contentOffset;
CGRect bounds = aScrollView.bounds;
CGSize size = aScrollView.contentSize;
UIEdgeInsets inset = aScrollView.contentInset;
float y = offset.y + bounds.size.height - inset.bottom;
float h = size.height;
// NSLog(@"offset: %f", offset.y);
// NSLog(@"content.height: %f", size.height);
// NSLog(@"bounds.height: %f", bounds.size.height);
// NSLog(@"inset.top: %f", inset.top);
// NSLog(@"inset.bottom: %f", inset.bottom);
// NSLog(@"pos: %f of %f", y, h);
float reload_distance = 10;
if(y > h + reload_distance) {
NSLog(@"load more rows");
}
}
你能提供更多的信息吗。就像在什么意义上你需要这个,实际的需求是什么,你尝试过什么方法?我从服务器获取数据,并将数据显示到我的tableviewcell中。但我想一次只加载10条记录。为此,我使用了限制偏移的东西。但我不太清楚如何在ios中做到这一点。我正在NSUrl中设置这些限制偏移。类似的问题:1。2.谢谢,这对我有用。只要一点帮助。当我加载新数据时,tableView的大小将增加。那么我如何相应地调整高度呢?因此,如果不需要,数据就不会加载。您需要根据可能提供解决方案的tableview框架的内容高度设置它。