UIScrollView custom paging size

后端 未结 11 1501
走了就别回头了
走了就别回头了 2020-12-07 11:49

paging in UIScrollView is a great feature, what I need here is to set the paging to a smaller distance, for example I want my UIScrollView to page less size that the UIScrol

11条回答
  •  感情败类
    2020-12-07 12:19

    Adding gesture recognizers or other subviews and so on is silly. Just set the delegate for the scroll view an imlement on of the below :

    // This is for a vertical scrolling scroll view. 
    // Let's say you want it to snap to every 160 pixels :    
    
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {     
         int y = scrollView.contentOffset.y;
         int yOff = y % 160;
         if(yOff < 80)
              y -= yOff;
         else
              y += 160 - yOff;
    
         [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, y) animated:YES];
    }
    
    // This is for a horizontal scrolling scroll view.
    // Let's say you want the same, to snap to every 160 pixels :
    
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
         int x = scrollView.contentOffset.x;
         int xOff = x % 160;
         if(xOff < 80)
              x -= xOff;
         else
              x += 160 - xOff;
    
         [scrollView setContentOffset:CGPointMake(x, scrollView.contentOffset.y) animated:YES];
    }
    

提交回复
热议问题