具有分页功能的UIScrollView的灵敏度/滚动速度 [英] Sensitivity/Scroll speed of UIScrollView with paging

查看:991
本文介绍了具有分页功能的UIScrollView的灵敏度/滚动速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望UIScrollView在快速扫描后快速滚动,这样
虽然,当打开分页时,滚动工作一次一页
是否可以快速滚动,只需轻轻一按手指启用分页而无需使用手势识别器手动实现?

I want UIScrollView to scroll fast after fast sweep, like this. Although, when paging is turned on, the scroll works one page at a time. Is it possible to scroll fast, when paging is enabled with a flick of a finger without manually implementation using gesture recognizers?

推荐答案

这是相当简单的,但是您必须自己实现分页方面(这非常简单)。你不需要手势识别器。

This is fairly straightforward, however you must implement the paging aspect yourself (which is fairly simple). You don't need gesture recognizers.

首先,调整 UIScrollView 减速率:

scrollView.decelerationRate = UIScrollViewDecelerationRateFast

假设我们有一系列内容 - 我们称之为 yourPagesArray 。在 UIScrollViewDelegate 中,实现以下方法:

Assume we have an array of content — let's call it yourPagesArray. In your UIScrollViewDelegate, implement the following method:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
{
    //This is the index of the "page" that we will be landing at
    let nearestIndex = Int(CGFloat(targetContentOffset.pointee.x) / scrollView.bounds.size.width + 0.5)

    //Just to make sure we don't scroll past your content
    let clampedIndex = max( min( nearestIndex, yourPagesArray.count - 1 ), 0 )

    //This is the actual x position in the scroll view
    var xOffset = CGFloat(clampedIndex) * scrollView.bounds.size.width

    //I've found that scroll views will "stick" unless this is done
    xOffset = xOffset == 0.0 ? 1.0 : xOffset

    //Tell the scroll view to land on our page
    targetContentOffset.pointee.x = xOffset
}

您还需要实现以下委托方法,以处理用户轻轻抬起手指而不会导致任何滚动减速的情况:

You will also need to implement the following delegate method, to handle the case where the user lifts their finger gently without causing any scroll deceleration:

(更新:您可能不需要在最新的iOS SDK下执行此操作。现在在速度为零时调用上面的委托方法。)

(Update: you may not need to do this under the latest iOS SDK. It seems like the above delegate method is now called when there is zero velocity.)

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
    if !decelerate
    {
        let currentIndex = floor(scrollView.contentOffset.x / scrollView.bounds.size.width)

        let offset = CGPoint(x: scrollView.bounds.size.width * currentIndex, y: 0)

        scrollView.setContentOffset(offset, animated: true)
    }
}



Objective-C



设置降低你的减速率:

Objective-C

Setting your deceleration rate:

scrollView.decelerationRate = UIScrollViewDecelerationRateFast;

然后你的滚动视图委托实现:

Then your scroll view delegate implementation:

- (void) scrollViewWillEndDragging:(UIScrollView *)scroll withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    //This is the index of the "page" that we will be landing at
    NSUInteger nearestIndex = (NSUInteger)(targetContentOffset->x / scrollView.bounds.size.width + 0.5f);   

    //Just to make sure we don't scroll past your content
    nearestIndex = MAX( MIN( nearestIndex, yourPagesArray.count - 1 ), 0 );

    //This is the actual x position in the scroll view
    CGFloat xOffset = nearestIndex * scrollView.bounds.size.width;

    //I've found that scroll views will "stick" unless this is done
    xOffset = xOffset==0?1:xOffset;

    //Tell the scroll view to land on our page
    *targetContentOffset = CGPointMake(xOffset, targetContentOffset->y);
}

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if( !decelerate )
    {
        NSUInteger currentIndex = (NSUInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width);

        [scrollView setContentOffset:CGPointMake(scrollView.bounds.size.width * currentIndex, 0) animated:YES];
    }
}

这篇关于具有分页功能的UIScrollView的灵敏度/滚动速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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