KVO难道不能同步工作吗?

时间:2014-10-21 17:58:15

标签: ios objective-c uitableview uiscrollview key-value-observing

我必须有一些关于KVO的东西,我试图通过滚动UIScrollView来滚动UITableView,偏移量转换是正确的,但UIScrollView滚动完成后会发生UITableView滚动。

这是我的观察代码:

- (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];
}

这是一个控制台日志,显示这是按预期工作的:

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)

但是当UIScrollView完成减速(并且UIScrollView委托方法中没有任何内容)时,UITableView仅在所有这些内容偏移计算后才开始滚动。 你知道为什么吗?

1 个答案:

答案 0 :(得分:4)

UITableViewUIScrollView的子类。当您为UIScrollView的滚动设置动画时,与其他大多数视图动画不同,它不会使用CAAnimation为更改设置动画。相反,它使用计时器。

我能够重现您的问题并进行调查。我相信你的问题是计时器只安排在运行循环的默认模式(kCFRunLoopDefaultMode)中运行,但是当用户拖动滚动视图时(以及当滚动视图在滚动视图后减速时)拖动),运行循环在UITrackingRunLoopMode。因此,在减速结束之前,表格视图的滚动计时器不会开始触发。

没有文档(或其他明显的)方法让表视图的计时器在其他运行循环模式下运行,但这似乎有效:

[UIView animateWithDuration:0.2 animations:^{
    self.tableView.contentOffset = CGPointMake(0, rect.origin.y);
}];

这在模拟器中对我有用,如iPhone 5(7.0.3)和iPhone 6(8.0)。

相关问题