Last Updated: February 25, 2016
·
3.93K
· domkck

UIScrollView snap to grid

Implementing snap to grid functionality of UIScrollView:

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    CGFloat target = targetContentOffset->x;
    //calculate new target
    CGFloat newTarget = newTargetFromTarget(target); 

    targetContentOffset->x = newTarget;
}

The targetContentOffset->x/y actually notifies the UIScrollView about the change and it scrolls to the new target instead.