UIScrollView自定义分页大小 [英] UIScrollView custom paging size
问题描述
谢谢
有一个 UIScrollView
委托方法你可以使用。将您的类设置为滚动视图的委托,然后实现以下内容:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset :( inout CGPoint *)targetContentOffset
{
CGFloat kMaxIndex = 23;
CGFloat targetX = scrollView.contentOffset.x + velocity.x * 60.0;
CGFloat targetIndex = round(targetX /(kCellWidth + kCellSpacing));
if(targetIndex< 0)
targetIndex = 0;
if(targetIndex> kMaxIndex)
targetIndex = kMaxIndex;
targetContentOffset-> x = targetIndex *(kCellWidth + kCellSpacing);
}
速度参数是确保滚动感觉自然而不是必需的参数当手指仍在移动时触摸结束时突然结束。单元格宽度和单元格间距是页面宽度和视图中页面之间的间距。在这种情况下,我使用 UICollectionView
。
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 UIScrollView frame width. Thanks
There is a UIScrollView
delegate method you can use. Set your class as the scroll view's delegate, and then implement the following:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
CGFloat kMaxIndex = 23;
CGFloat targetX = scrollView.contentOffset.x + velocity.x * 60.0;
CGFloat targetIndex = round(targetX / (kCellWidth + kCellSpacing));
if (targetIndex < 0)
targetIndex = 0;
if (targetIndex > kMaxIndex)
targetIndex = kMaxIndex;
targetContentOffset->x = targetIndex * (kCellWidth + kCellSpacing);
}
The velocity parameter is necessary to make sure the scrolling feels natural and doesn't end abruptly when a touch ends with your finger still moving. The cell width and cell spacing are the page width and spacing between pages in your view. In this case, I'm using a UICollectionView
.
这篇关于UIScrollView自定义分页大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!