我希望UIScrollView在快速扫描后快速滚动,like this。 虽然打开页面时,滚动工作one page at a time。 当使用手指轻拂启用分页而不使用手势识别器进行手动实现时,是否可以快速滚动?带分页的UIScrollView的灵敏度/滚动速度
22
A
回答
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;
}
相关问题
- 1. 调整qooxdoo滚动速度/灵敏度
- 2. 调整UICollectionView滚动速度/灵敏度
- 3. 你可以改变UIScrollView滚动的灵敏度吗?
- 4. Jquery JScrollPane - 设置滚动速度/灵敏度
- 5. 更改UIScrollView滚动速度
- 6. 如何降低UIScrollview的滚动速度?
- 7. iScroll的灵敏度
- 8. Pyomo灵敏度分析
- 9. 滑动效果灵敏度
- 10. UIScrollview scrollViewShouldScrollToTop - 减慢滚动速度
- 11. iPhone,iPods,iPad加速度计的灵敏度?
- 12. RecyclerView带偏移量的滚动速度
- 13. 从TapkuLibrary降低Coverflow的滚动灵敏度
- 14. 加速度计在不同的倾斜度下有不同的灵敏度?
- 15. 更改UIScrollView拖动速度
- 16. ds触摸屏灵敏度
- 17. 麦克风灵敏度
- 18. iPhone滑块灵敏度
- 19. JQTouch压力灵敏度
- 20. 我们能赶上的UIScrollView的默认滚动速度更快
- 21. Python滚动速度
- 22. jquery滚动速度?
- 23. jQuery的滚动速度
- 24. iOS梯度动画与uiscrollview分页
- 25. 提高AI精灵的移动速度
- 26. 如何使UIScrollView以特定速度自动滚动?
- 27. 以页面滚动的不同速度滚动项目
- 28. 如何以编程方式控制加速度传感器的灵敏度?
- 29. 调整Android中setOnTouchListner的灵敏度
- 30. Box2d - 降低b2body的灵敏度
有没有找到解决方案呢? – Eric 2012-08-29 22:45:31
我希望有一个很好的解决方案。看起来有两种实现方法:手动实现手势识别器或手动实现分页。 – UrK 2012-08-30 05:29:26
是的这可能是我在6个月前实施的。你可以手动做手势识别器。 – JohnWhite 2012-09-17 08:43:44