gpt4 book ai didi

cocoa-touch - 预测减速后 UIScrollView 中的静止偏移量

转载 作者:行者123 更新时间:2023-12-03 10:42:56 25 4
gpt4 key购买 nike

我希望能够在轻弹手势后预测 UIScrollView 内的最终静止偏移量。它不需要像素精确,但足够接近,以便用户不会感觉到差异(即它不会比他们习惯的移动得过少或过多)。

我知道有人会问,所以:为什么?我在 UIScrollView 中有一个类似于表格 View 的菜单控件。我想让最顶部的菜单项完全显示并刷新到 UIScrollView 的顶部。 UIScrollView 的分页功能并不是我想要的,因为强烈的轻弹不会飞越 View 边界的倍数。

处理正常的触摸事件很容易。上 touchesEnded:withEvent: ,我可以滚动到最近的完整菜单项。困难的部分是减速。

减速率有两个常数,UIScrollViewDecelerationRateNormalUIScrollViewDecelerationRateFast .它们的值在 iPhone OS 3.0 中分别为 0.998 和 0.990。我试图找出 Apple 用来减慢移动速度的数学方法,但结果却一无所获。

如果我能准确地预测最终的静止偏移,那么在减速的早期我可以简单地使用 scrollRectToVisible:animated:移动到一个菜单项与 View 边界顶部齐平的偏移量。

有没有喜欢数学的人知道苹果在减速期间可能会做什么?我应该收集一堆减速事件的数量,绘制它们并得出一些接近的结果吗?

最佳答案

在 iOS 5 中控制 UIScrollView 的 targetContentOffset

iOS5 UIScrollViewDelegate 有一个新方法: scrollViewWillEndDragging:withVelocity:targetContentOffset: .

这完全符合您想要做的事情。

This method is not called when the value of the scroll view’s pagingEnabled property is YES. Your application can change the value of the targetContentOffset parameter to adjust where the scrollview finishes its scrolling animation.



iOS4 及更低版本的替代解决方案

更改 UIScrollView decelerationRate UIScrollViewDecelerationFast 然后在 scrollViewDidEndDecelerating 移动到最近的“页面”。

快速减速使完全停止/滑过更自然/不那么令人讨厌。

关于cocoa-touch - 预测减速后 UIScrollView 中的静止偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1651560/

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