预测减速后 UIScrollView 中的静止偏移量 [英] Predicting the resting offset in a UIScrollView after deceleration

查看:16
本文介绍了预测减速后 UIScrollView 中的静止偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

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

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.

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

Handling normal touch events is easy enough. On touchesEnded:withEvent:, I can scroll to the nearest full menu item. The hard part is deceleration.

减速率有两个常数,UIScrollViewDecelerationRateNormalUIScrollViewDecelerationRateFast.在 iPhone OS 3.0 中,它们的值为 0.998 和 0.990.我试图弄清楚 Apple 用来减缓移动速度的数学方法,但我一无所获.

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.

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

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?

推荐答案

在iOS 5中控制UIScrollView的targetContentOffset

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

这完全符合你想要做的.

This fits perfectly with what you want to do.

当滚动视图的值pagingEnabled 属性为 YES.您的应用程序可以更改该值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 及更低版本的替代解决方案

更改UIScrollViewdecelerationRateUIScrollViewDecelerationFast 然后在 scrollViewDidEndDecelerating 移动到最近的页面".

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆