【问题标题】:Check if uitableviewcell is 30% visible when going offscreen while User scrolling检查 uitableviewcell 在用户滚动时离开屏幕时是否有 30% 可见
【发布时间】:2021-12-22 15:21:52
【问题描述】:

当用户滚动 uitableview 时,有什么方法可以知道到达顶部时,许多 uitableviewcell 之一何时离屏 70%?我应该为此使用 willdisplaycell 吗?任何帮助表示赞赏。

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    实现scrollViewDidScroll...

    • 获取要跟踪的行/单元格的框架
    • 将其与滚动偏移量进行比较,看看有多少是可见的

    在这个简单的例子中,rowToTrack 是一个NSInteger,例如第三行的2sectionToTrack 是一个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);
        
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      相关资源
      最近更新 更多