Want UITableView to “snap to cell”

前端 未结 7 1618
长发绾君心
长发绾君心 2020-12-23 17:40

I am displaying fairly large images in a UITableView. As the user scrolls, I\'d like to the table view to always snap the center-most photo in the middle. That

7条回答
  •  感动是毒
    2020-12-23 18:10

    Building from what @jszumski posted, if you want the snap to occur mid drag, use this code:

    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
        [self centerTable];
    }
    
    - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
        [self centerTable];
    }
    
    - (void)centerTable {
        NSIndexPath *pathForCenterCell = [self.tableView indexPathForRowAtPoint:CGPointMake(CGRectGetMidX(self.tableView.bounds), CGRectGetMidY(self.tableView.bounds))];
    
        [self.tableView scrollToRowAtIndexPath:pathForCenterCell atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
    

提交回复
热议问题