gpt4 book ai didi

ios - 带有分页的 UIScrollView 的灵敏度/滚动速度

转载 作者:IT王子 更新时间:2023-10-29 07:52:37 29 4
gpt4 key购买 nike

我希望 UIScrollView 在快速扫描后快速滚动,like this .虽然,当打开分页时,滚动有效 one page at a time .如果无需使用手势识别器手动实现,只需轻弹手指即可启用分页,是否可以快速滚动?

最佳答案

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

swift

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

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

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

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;

然后你的 ScrollView 委托(delegate)实现:

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

关于ios - 带有分页的 UIScrollView 的灵敏度/滚动速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11561992/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com