There must be something I am not getting about KVO, I am trying to scroll a UITableView by scrolling a UIScrollView, the offset translations are correct but the UITableView scrolling occurs after the UIScrollView has finished scrolling.
Here is my observing code:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
AILog(@"observeValueForKeyPath:%@ ofObject:%@", keyPath, NSStringFromClass([object class]));
static BOOL isObservingContentOffsetChange = NO;
if ([keyPath isEqualToString:@"contentOffset"]) {
if ([object isEqual:self.scrollView]) {
if (isObservingContentOffsetChange) {
return;
}
isObservingContentOffsetChange = YES;
CGPoint offset = [[change valueForKey:NSKeyValueChangeNewKey] CGPointValue];
NSDate *offsetDay = [self dayForScrollViewOffset:offset];
if (offsetDay) {
AILog(@"offset (%.2f, %.2f)", offset.x, offset.y);
AILog(@"offsetDay %@", [df stringFromDate:offsetDay]);
NSIndexPath *offsetIndexPath = [self indexPathForDay:offsetDay];
AILog(@"offsetIndexPath (%d, %d)", (int)offsetIndexPath.section, (int)offsetIndexPath.row);
if (offsetIndexPath) {
CGRect rect = [self.tableView rectForSection:offsetIndexPath.section];
[self.tableView setContentOffset:CGPointMake(0, rect.origin.y) animated:YES];
AILog(@"----------------------------------------------");
}
} else {
AILog(@"nil offset day");
}
}
isObservingContentOffsetChange = NO;
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
And here is a console log which show this is working as expected:
014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1526.50, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Sunday, 19/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (2, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1520.00, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Sunday, 19/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (2, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1514.00, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Sunday, 19/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (2, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1508.50, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Sunday, 19/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (2, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1503.50, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Sunday, 19/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (2, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1498.00, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Sunday, 19/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (2, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1493.00, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Sunday, 19/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (2, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1488.00, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Sunday, 19/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (2, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1483.50, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Saturday, 18/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (3, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1479.00, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Saturday, 18/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (3, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1474.50, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Saturday, 18/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (3, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1470.00, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Saturday, 18/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (3, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1466.00, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Saturday, 18/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (3, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1462.00, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Saturday, 18/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (3, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1458.50, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Saturday, 18/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (3, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1454.50, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Saturday, 18/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (3, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1450.50, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Saturday, 18/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (3, 0)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> ----------------------------------------------
2014-10-21 17:44:53 +0000 : BPTimeViewController --> observeValueForKeyPath:contentOffset ofObject:UIScrollView
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offset (1447.00, 0.00)
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetDay Saturday, 18/10/2014
2014-10-21 17:44:53 +0000 : BPTimeViewController --> offsetIndexPath (3, 0)
But the UITableView starts scrolling only after all those content offsets calculation, when UIScrollView has finished decelerating (and there is nothing in UIScrollView delegate methods). Do you know why so?