UIScrollView自定义分页大小 [英] UIScrollView custom paging size

查看:558
本文介绍了UIScrollView自定义分页大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UIScrollView中分页是一个很棒的功能,我需要的是将分页设置为较小的距离,例如我希望我的UIScrollView页面的大小小于UIScrollView的帧宽。
谢谢

解决方案

有一个 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆