【问题标题】:UITableView scrollViewDidScroll fired on animationUITableView scrollViewDidScroll 触发动画
【发布时间】:2014-05-11 17:37:01
【问题描述】:

所以人们对这个特定事件的大多数问题是在动画发生时让scrollViewDidScroll 触发。我的情况正好相反。我觉得 scrollViewDidScroll 在我的情况下不应该被解雇。

让我进一步解释。

我正在为 scrollViewDidScroll 中的东西设置动画,在我将 UITableView 移动到 UIView 类之前,它一直运行良好。

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
   // Animation code here.
   NSLog(@"scrollViewDidScroll");
}

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
   NSLog(@"scrollViewDidEndDecelerating");
   NSArray *indexPaths = [_myTableView indexPathsForVisibleRows];
   [_myTableView scrollToRowAtIndexPath:[indexPaths objectAtIndex:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}


这提供了一种平滑的滚动体验,可以快速返回到前一个表格行。控制台可以验证scrollViewDidScroll 事件是因为scrollToRowAtIndexPath 而触发的。

控制台:

2014-03-31 22:21:43.346 Project[45843:a0b] scrollViewDidScroll
2014-03-31 22:21:43.379 Project[45843:a0b] scrollViewDidScroll
2014-03-31 22:21:43.429 Project[45843:a0b] scrollViewDidScroll
2014-03-31 22:21:43.462 Project[45843:a0b] scrollViewDidScroll
2014-03-31 22:21:43.479 Project[45843:a0b] scrollViewDidEndDecelerating
2014-03-31 22:21:43.496 Project[45843:a0b] scrollViewDidScroll
2014-03-31 22:21:43.513 Project[45843:a0b] scrollViewDidScroll
2014-03-31 22:21:43.529 Project[45843:a0b] scrollViewDidScroll

关于问题:

1.如何确保事件 scrollViewDidScroll 仅因用户交互而非代码自动化而触发?

2.是否有另一种方法可以提供与scrollToRowAtIndexPath 相同的功能而不触发scrollViewDidScroll

【问题讨论】:

  • 据我所知,如果没有调用委托方法,您将无法滚动。您可以做的一件事是通过以编程方式滚动,您可以设置一个标志并在委托方法中使用它。如果您以编程方式滚动重置标志。我希望这会对你有所帮助..

标签: ios objective-c uitableview ios7 uiscrollview


【解决方案1】:

我知道这是一个非常古老的问题。我刚刚遇到过类似的情况,我只需要在用户滚动时运行一些代码,而在我调用scrollToRowAtIndexPath 时不需要。我使用滚动视图变量draggingdecelerating 找到了一个更简单的解决方案。

我们这里有3个滚动案例。

  1. 用户正在拖动
  2. 用户刚刚结束拖动,tableview 正在减速
  3. 以编程方式调用scrollForRowAtIndexPath:

以上所有情况都会触发scrollViewDidScroll:但是

  1. 用户正在拖动 draggingTrue 并且 deceleratingFalse
  2. 用户刚刚结束拖动,tableview 正在减速 draggingFalse 并且 deceleratingTrue
  3. 以编程方式调用 scrollForRowAtIndexPath: draggingFalse 并且 deceleratingFalse

所以你的代码会是这样的:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.dragging)
     {
       // Do what you want when user is dragging
     } else if (scrollView.decelerating) 
     {
       // Do what you want when table is decelerating after finished dragging
     } else 
     {
       // Do what you want when table is scrolling programmatically.
     }
}

或者,如果您只想以编程方式区分滚动

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.dragging || scrollView.decelerating)
     {
       // Scrolling by the user
     } else 
     {
       // Scrolling by the code
     }
}

【讨论】:

  • 这是一个比我自己滚动更优雅的解决方案!将此更改为接受的答案。
【解决方案2】:

在我发布问题后不久,我稍作休息,回到问题并想通了。应该尽快完成,而不是浪费几个小时。呸!

解决方案很简单,设置一个bool 标志,该标志在任何程序滚动之前设置,然后在动画完成后使用事件scrollViewDidEndScrollingAnimation 更改它。

bool performingAutomatedScroll = false;

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {

  // If we are scrolling because of code, don't use any animations.
  if (performingAutomatedScroll) return;

  // Animation code here.
}

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

  performingAutomatedScroll = true;
  NSArray *indexPaths = [_myTableView indexPathsForVisibleRows];
  [_myTableView scrollToRowAtIndexPath:[indexPaths objectAtIndex:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
  [_myTableView reloadRowsAtIndexPaths:[_timeCarousel indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];

}

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

  if (!decelerate) {
    performingAutomatedScroll = true;
    NSArray *indexPaths = [_myTableView indexPathsForVisibleRows];
    [_myTableView scrollToRowAtIndexPath:[indexPaths objectAtIndex:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    [_myTableView reloadRowsAtIndexPaths:[_timeCarousel indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
  }

}

-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
  performingAutomatedScroll = false;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 2010-11-06
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多