Sensitivity/Scroll speed of UIScrollView with pagi

2019-06-26 09:51发布

我想要的UIScrollView来快速扫描后快速滚动, 像这样 。 虽然,当呼叫接通时,滚动运作一次一个页面 。 是否可以滚动快速,呼叫时用手指轻弹启用,而无需手动使用手势识别的实现?

Answer 1:

这是相当简单的,但是你必须实现自己的寻呼方面(这是相当简单)。 你不需要手势识别。

迅速

首先,调整你的UIScrollView的减速率:

scrollView.decelerationRate = UIScrollViewDecelerationRateFast

假设我们有内容的阵列-姑且称之为yourPagesArray 。 在你UIScrollViewDelegate,实现以下方法:

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
}

您还需要实现以下的委托方法,来处理该用户轻轻地提起他们的手指,而不会造成任何滚动减速的情况下:

(更新:你可能不需要这么做了最新的iOS SDK下,这似乎是上述委托方法时,有零速度现在叫)

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的

设置你的减速度:

scrollView.decelerationRate = UIScrollViewDecelerationRateFast;

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

- (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];
    }
}


Answer 2:

我认为,我们可以设置scrollView.userInteractionEnabled = NO,当我们的手指触摸它。 然后当动画停止,打开它,对我来说。它的工作原理。 希望它会帮助你。

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    scrollView.userInteractionEnabled = NO;
}

// at the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    scrollView.userInteractionEnabled = YES;
}


文章来源: Sensitivity/Scroll speed of UIScrollView with paging