ios - Track coordinates of UITableViewCell subview -
i fire event when subview of uitableviewcell reaches point on screen, example when origin.y reaches 44 points. nice know if being scrolled or down when reached point. playing kvo on frame of subview seems fixed cell no changes that. task possible?
vertical position of uitableviewcell defined frame
property, represents position , size of cell within superview, uitableview. typically, frame
property of cell changing once every time uitableview requests cell delegate specific index path. that's it, uitableview gets cell, places in , cell lays there unchanged until rectangle stored in bounds
property of uitableview ceases include rectangle stored in frame
property of cell. in case uitableview marks cell hidden , places pool of reusable cells.
since process of scrolling in essence not repositioning of subviews – merely curious illusion of shifting bounds
viewport of uitableview – constant observing of uitableviewcell's properties pointless.
moreover, frame
property of subview of uitableviewcell represents position , size of subview within container, uitableviewcell. not change on scroll.
you need observe changes in uitableview bounds
property, represented contentoffset
way. uitableview happens subclass of uiscrollview, can use delegate methods, such -scrollviewdidscroll:
, in simple example:
- (void)scrollviewdidscroll:(uiscrollview *)scrollview { uitableview *tableview = (uitableview *)scrollview; // current position cgfloat currenty = tableview.bounds.origin.y; // current inset cgfloat currentinset = tableview.contentinset.top; // trigger line position cgfloat triggery = currentinset + currenty + kyourtriggerposition; // nice visual mark uiview *line = [tableview viewwithtag:88]; if (!line) { line = [[uiview alloc] initwithframe:cgrectzero]; line.tag = 88; line.backgroundcolor = [uicolor redcolor]; [tableview addsubview:line]; } line.frame = cgrectmake(0, triggery, tableview.bounds.size.width, 1); // determine scroll direction bool scrollingup = currenty > self.previousy; // visible cells nsarray *visiblecells = tableview.visiblecells; [visiblecells enumerateobjectsusingblock:^(uitableviewcell *cell, nsuinteger idx, bool *stop) { // subview uiview *subview = [cell viewwithtag:kyoursubviewtag]; // subview frame rect in uitableview bounds cgrect subviewrect = [subview convertrect:subview.frame toview:tableview]; // trigger line within subview? bool triggered = (cgrectgetminy(subviewrect) <= triggery) && (cgrectgetmaxy(subviewrect) >= triggery); if (triggered) { nsindexpath *indexpath = [tableview indexpathforcell:cell]; nslog(@"moving %@, triggered cell @ [%2d:%2d]", @[@"down", @"up"][scrollingup], indexpath.section, indexpath.row); [tableview selectrowatindexpath:indexpath animated:no scrollposition:uitableviewscrollpositionnone]; } }]; // save current position future use self.previousy = currenty; }
Comments
Post a Comment