【问题标题】:Amount of scrolling in scrollViewWillBeginDraggingscrollViewWillBeginDragging 中的滚动量
【发布时间】:2013-09-06 09:42:43
【问题描述】:

有什么方法可以找到 UITableView 在任何方向上滚动了多少?我感兴趣的是数量而不是方向。

【问题讨论】:

  • 您能否提供更多信息。比如你在什么意义上需要这个,实际的要求是什么,你尝试过的方法是什么?
  • 我正在从服务器获取数据并将该数据显示到我的 tableviewcell 中。但我想一次只加载 10 条记录。为此,我正在使用限制偏移量。但我不太清楚如何在 ios 中执行此操作。我在 NSUrl 中设置那些限制偏移量。

标签: iphone ios uitableview uiscrollviewdelegate


【解决方案1】:

您可以通过查看 table view 的 contentOffset 属性轻松获取 table view 的确切偏移量。对于垂直滚动,请看:

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");
    }
}

【讨论】:

  • 感谢这对我有用。只是一点点帮助。当我加载新数据时,tableView 的大小会增加。那么我怎样才能相应地调整高度呢?因此,如果不需要,数据不会被加载。
  • 你需要根据它的内容高度设置tableview框架,这可能会给你解决方案。
【解决方案2】:

您需要在拖动开始和结束时测量UITableViewcontentOffset。取两者之间的差异,它会给你从初始位置到最终位置的变化量。

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
}

希望有帮助!

【讨论】:

    【解决方案3】:

    您可以使用以下语法找到它...

    NSLog(@"scrolled:%f",yourtableview.contentOffset.y);//y for vertical, x for horizontal
    

    【讨论】:

      猜你喜欢
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多