我希望 UIScrollView 在快速扫描后快速滚动,就像这样。虽然,当打开分页时,滚动一次只能工作一页。当通过手指轻弹启用分页而不使用手势识别器手动实现时,是否可以快速滚动?
问问题
31924 次
2 回答
59
这相当简单,但是您必须自己实现分页方面(这相当简单)。您不需要手势识别器。
迅速
首先,调整你的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];
}
}
于 2012-12-13T10:41:05.623 回答
4
我想我们可以在手指触摸它时设置 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;
}
于 2013-11-11T12:37:43.370 回答