It appears self.decelerating and self.dragging are not reliable during an overridden layoutSubviews call. For example, they are occasionally both true, which is obviously not possible.

Is there a reliable way to get whether the UIScrollView is decelerating in layoutSubviews?

有帮助吗?

解决方案 2

Having an instance variable set like so fixed it:

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    _decelerating = YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    _decelerating = NO;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _decelerating = NO;
}

The issue is if you start dragging while it's decelerating, self.decelerating is not unset. The above fixes that by taking into account scrollViewWillBeginDragging.

其他提示

I've never had any issues with the UIScrollView protocol methods being unreliable. Is it possible for you to manage whatever your doing currently in layoutSubviews from another delegate class? Like a parent view, or something?

Then you could listen for the delegate methods with (potentially) more reliability.

#pragma mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  if (self.scrollViewDecelerating)
  {
    // stuff...
  }
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
  self.scrollViewDecelerating = YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
  self.scrollViewDecelerating = NO;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top