2012-07-19 131 views
22

我希望UIScrollView在快速扫描后快速滚动,like this。 虽然打开页面时,滚动工作one page at a time。 当使用手指轻拂启用分页而不使用手势识别器进行手动实现时,是否可以快速滚动?带分页的UIScrollView的灵敏度/滚动速度

+0

有没有找到解决方案呢? – Eric 2012-08-29 22:45:31

+0

我希望有一个很好的解决方案。看起来有两种实现方法:手动实现手势识别器或手动实现分页。 – UrK 2012-08-30 05:29:26

+0

是的这可能是我在6个月前实施的。你可以手动做手势识别器。 – JohnWhite 2012-09-17 08:43:44

回答

40

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

斯威夫特

首先,调整的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]; 
    } 
} 
1

我认为,我们可以设置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; 
}