具有分页function的UIScrollView的灵敏度/滚动速度

快速扫描之后,我希望UIScrollView快速滚动, 就像这样 。 虽然打开页面时 ,滚动一次只能打印一页 。 是否可以快速滚动,当使用手指滑动启用分页时,无需使用手势识别器手动执行?

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

迅速

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

我想我们可以设置scrollView.userInteractionEnabled =否当我们的手指触摸它。 然后,当animation停止时,打开它,它为我工作。 希望它会帮助你。

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