ios 带有分页的 UIScrollView 的灵敏度/滚动速度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11561992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sensitivity/Scroll speed of UIScrollView with paging
提问by UrK
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?
我希望 UIScrollView 在快速扫描后快速滚动,就像这样。虽然,当分页打开时,滚动一次一页。是否可以快速滚动,当无需使用手势识别器手动实现时,只需轻弹手指即可启用分页?
回答by simeon
This is fairly straightforward, however you must implement the paging aspect yourself (which is fairly simple). You don't need gesture recognizers.
这相当简单,但是您必须自己实现分页方面(这相当简单)。您不需要手势识别器。
Swift
迅速
First, adjust your UIScrollViewdeceleration rate:
首先,调整你的UIScrollView减速率:
scrollView.decelerationRate = UIScrollViewDecelerationRateFast
Assume we have an array of content — let's call it yourPagesArray
. In your UIScrollViewDelegate, implement the following method:
假设我们有一个内容数组——我们称之为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
}
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:
您还需要实现以下委托方法,以处理用户轻轻抬起手指而不会导致任何滚动减速的情况:
(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.)
(更新:在最新的 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
目标-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];
}
}
回答by Hilen
I think we can set scrollView.userInteractionEnabled = NO when our finger touch it. And then when the animation is stop ,open it .It works for me. Hope it will help you.
我认为我们可以在手指触摸时设置 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;
}