当用户滚动uitableview时,有什么方法可以知道当众多uitableview单元格中的一个单元格到达顶部时,屏幕外的比例是70%吗?我应该使用willdisplaycell吗?感谢您的帮助。
实现scrollViewDidScroll
。。。
- 获取要跟踪的行/单元格的帧
- 将其与滚动偏移量进行比较,查看其中有多少是可见的
在这个简单的例子中,rowToTrack
是NSInteger
,例如第三行的2
,sectionToTrack
是NSInteger
,例如第一节的0
:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// index path for row we want to track
NSIndexPath *p = [NSIndexPath indexPathForRow:rowToTrack inSection:sectionToTrack];
// get frame for that row
CGRect r = [self.tableView rectForRowAtIndexPath:p];
// get bottom of row frame
CGFloat t1 = CGRectGetMaxY(r);
// get scroll content offset
CGFloat t2 = scrollView.contentOffset.y + scrollView.adjustedContentInset.top;
// height of visible portion of row frame
CGFloat visibleHeight = t1 - t2;
// as a percentage between 0 and 1
CGFloat pct = MAX(0.0, MIN(1.0, visibleHeight / r.size.height));
NSLog(@"Row %ld is %0.2f percent visible", rowToTrack, pct * 100);
}