ios - Shouldn't KVO work syncronously? -
there must not getting kvo, trying scroll uitableview scrolling uiscrollview, offset translations right uitableview scrolling occurs after uiscrollview has finished scrolling.
here 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 console log show working 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 uitableview starts scrolling after content offsets calculation, when uiscrollview has finished decelerating (and there nil in uiscrollview delegate methods). know why so?
uitableview
subclass of uiscrollview
. when animate scrolling of uiscrollview
, not utilize caanimation
animate change, unlike other view animations. instead, uses timer.
i able reproduce problem , investigate. believe problem timer scheduled run in run loop's default mode (kcfrunloopdefaultmode
), when user dragging scroll view (and when scroll view decelerating after drag), run loop in uitrackingrunloopmode
. table view's scroll timer doesn't begin firing until deceleration ends.
there no documented (or otherwise obvious) way table view's timer run in other run loop modes, seems work:
[uiview animatewithduration:0.2 animations:^{ self.tableview.contentoffset = cgpointmake(0, rect.origin.y); }];
this worked me in simulator iphone 5 (7.0.3) , iphone 6 (8.0).
ios objective-c uitableview uiscrollview key-value-observing
No comments:
Post a Comment