سؤال

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