简体   繁体   English

如何在我自己的自定义视图中重现iPhone UIScrollView的轻弹滚动行为?

[英]How can I reproduce the flick-to-scroll behavior of the iPhone UIScrollView in my own custom view?

I would like to reproduce UIScrollView's flick-to-scroll behavior, but I don't want to use (or can't use) that class. 我想重现UIScrollView的flick-to-scroll行为,但我不想使用(或不能使用)该类。 What can I do? 我能做什么?

Okay, I've answered this by implementing my own library that captures the dynamics of UIScrollView. 好的,我通过实现自己的库捕获了UIScrollView的动态来回答这个问题。

What's useful about my code is that it is independent of coordinate system, animation rate, and particular UI classes that you're using. 我的代码有用的是它独立于你正在使用的坐标系,动画速率和特定的UI类。 It can easily be nested in a custom view class of your choosing. 它可以轻松嵌套在您选择的自定义视图类中。

The iPhone's default flick-to-scroll behavior is interesting. iPhone的默认轻弹滚动行为很有趣。 If you hold your finger down for a long time and move it in a variety of directions, you'll see that only the very last direction is used to compute the scrolling motion. 如果长时间按住手指并沿各个方向移动,您将看到只有最后一个方向用于计算滚动动作。

If you try to build this code yourself, however, you'll quickly discover that simply using the last two points to compute the direction of motion isn't going to cut it. 但是,如果您尝试自己构建此代码,您很快就会发现仅使用最后两个点来计算运动方向并不会削减它。 Instead, my code keeps a short history of touches and uses linear interpolation to determine where the touch “would have been” some small amount of time ago. 取而代之的是,我的代码保留了短暂的触摸历史,并使用线性插值来确定触摸“本来会在不久之前”的位置。 This interpolated point is used as the basis for computing the motion vector. 该插值点用作计算运动矢量的基础。 This leads to a very pleasing interaction. 这导致了非常愉快的互动。

The code is available on github, here: http://gist.github.com/100855 . 该代码可在github上找到, 网址为http//gist.github.com/100855 It is released under the BSD license. 它是根据BSD许可证发布的。

This is cool! 这很酷! I've used it in an app where my scrollable view is one day's worth of graphical data in a range from 1902-2037, so a UIScrollView would not be efficient. 我在一个应用程序中使用它,我的可滚动视图是一天的图形数据,范围从1902年至2037年,因此UIScrollView效率不高。

So what about UIScrollView did not work for you? 那么UIScrollView对你不起作用呢? That class is pretty flexible... 那堂课非常灵活......

Dave, I see you've answerd your own question with some code but, doesn't setting the following give you what you need: 戴夫,我看到你用一些代码回答了你自己的问题但是,没有设置以下内容给你所需要的东西:

myScrollView.bounces = YES;
myScrollView.pagingEnabled = YES;
myScrollView.directionalLockEnabled = YES;

Specifically, it sounds like you have reimplemented directionLockEnabled . 具体来说,听起来你已经重新实现了directionLockEnabled Maybe understanding why you can't use UIScrollView is the more interesting problem :-) 也许理解为什么你不能使用UIScrollView是更有趣的问题:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM