减速后预测UIScrollView中的静止偏移 [英] Predicting the resting offset in a UIScrollView after deceleration
问题描述
我想能够预测在滑动手势后UIScrollView中的最终休息偏移。它不需要像素精确,但足够接近,使得用户不会感觉到差异(即,它不会比他们过去的移动过少或更多)。
我知道有人会问,为什么?我有一个表视图像菜单控件内UIScrollView。我想让它,使最顶层的菜单项完全显示,并冲洗到UIScrollView的顶部。 UIScrollView的分页功能不是我想要的,因为强烈的轻弹不会飞过视图边界的倍数。
处理正常的触摸事件很容易。在 touchesEnded:withEvent:
,我可以滚动到最近的完整菜单项。
减速率有两个常数: UIScrollViewDecelerationRateNormal
和 UIScrollViewDecelerationRateFast
。在iPhone OS 3.0中,它们的值为0.998和0.990。我试图找出苹果用来减慢运动的数学,但我是空的。
如果我能以一定的精度预测最终的休息偏移,那么在减速期间,我可以简单地使用 scrollRectToVisible:animated:
移动到一个偏移量,菜单项刷新到视图边界的顶部。
任何数学倾向的人都知道苹果在减速过程中可能做什么?
在iOS中控制UIScrollView的targetContentOffset 5
iOS5 UIScrollViewDelegate
有一个新方法: scrollViewWillEndDragging:withVelocity:targetContentOffset:
。 >
这完全符合您想要的操作。
滚动视图的
pagingEnabled属性的值为YES。您的应用程序可以更改targetContentOffset参数的值
,以调整scrollview
完成滚动动画的位置。
iOS4和更低版本的替代解决方案
更改 UIScrollView
的 decelerationRate
到UIScrollViewDecelerationFast
,然后在 scrollViewDidEndDecelerating
移动到最近的页面。
快速减速使得完全停止/滑过一点更自然/更不讨厌。
I would like to be able to predict the final resting offset within a UIScrollView after a flick gesture. It doesn't need to be pixel-accurate, but close enough so that the user does not perceive a difference (i.e. it doesn't move excessively less or more than they are used to).
I know someone will ask, so: Why? I have a table view-like menu control inside a UIScrollView. I would like to make it such that the top-most menu item is fully displayed and flush to the top of the UIScrollView. UIScrollView's paging feature is not quite what I want, because a strong flick doesn't fly past multiples of the view bounds.
Handling normal touch events is easy enough. On touchesEnded:withEvent:
, I can scroll to the nearest full menu item. The hard part is deceleration.
There are two constants for the deceleration rate, UIScrollViewDecelerationRateNormal
and UIScrollViewDecelerationRateFast
. Their values are 0.998 and 0.990 in iPhone OS 3.0. I have tried to figure out the math that Apple uses to slow movement, but I'm coming up empty.
If I can predict with some accuracy the final resting offset, then early during deceleration I can simply use scrollRectToVisible:animated:
to move to an offset with a menu item flush to the top of the view bounds.
Do any math-inclined people know what Apple may be doing during deceleration? Should I collect a bunch of numbers of deceleration events, graph them and come up with something close?
Control UIScrollView's targetContentOffset in iOS 5
iOS5 UIScrollViewDelegate
has a new method: scrollViewWillEndDragging:withVelocity:targetContentOffset:
.
This fits perfectly with what you want to do.
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.
Alternative Solution for iOS4 and Lower
Change the UIScrollView
's decelerationRate
to UIScrollViewDecelerationFast
and then in scrollViewDidEndDecelerating
move to the closest "page".
The fast deceleration makes the complete stopping / sliding over a little more natural / less obnoxious.
这篇关于减速后预测UIScrollView中的静止偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!