拖动UIView部分方式,然后它自己移动 [英] Drag a UIView part-way, then it moves on its own

查看:119
本文介绍了拖动UIView部分方式,然后它自己移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,底部有一个可拖动的UIView。可拖动的视图不完全在屏幕外,并且具有用户可以向上或向下拖动的拉片。目前可以上下拖动,但我想给它提供与Apple通知滑出抽屉相同的行为。

I have an app with a draggable UIView placed at the bottom. The draggable view isnt totally offscreen and it has a "pull tab" that the user can drag upwards or downwards. Dragging up and down currently works, but I would like to give it the same behavior as the Apple notifications slide out drawer.

例如,如果我拖出视图50 %向上并从屏幕上移开我的手指,然后我希望可拖动的视图继续自己向上移动。同样,如果用户只拖出视图,比如说向上30%,那么视图应该回落到默认位置。

For example, if I drag the view out 50% upwards and remove my finger from the screen, then I'd like the draggable view to continue to move upwards on its own. Likewise, if the user only dragged the view out, say 30% upwards, then the view should drop back down to its default position.

理想情况下,虽然我可以做到向上/向下拖动,动作不是非常有机....

Ideally, while I can do the dragging up/down, the motion isnt very "organic"....

现在,我正在通过UIPanGestureRecognizer完成向上和向下拖动,以防万一与问题相关。

Right now, I'm accomplishing the dragging upwards and downwards via UIPanGestureRecognizer, just in case that's relevant to the question.

这可能与可拖动视图的Y位置的某些聪明的数学有关,然后使用一些CAAnimations进行其余的移动?

Is it perhaps something along the lines of some clever math with the Y position of the draggable view, then doing the rest of the moving with some CAAnimations?

可能有点难以想象,所以我在下面添加了一些屏幕。

It might be a bit hard to visualize, so I've added some screens below.

< a href =http://dl.dropbox.com/u/762437/screens/default.jpg =nofollow>默认屏幕,底部有一个视图

视图通过上面的标签向上拖动正确

谢谢!

推荐答案

UIPanGestureRecognizer 的状态变为 UIGestureRecognizerStateEnded 时,请使用 velocityInView:消息,用于查找手势的速度。

When your UIPanGestureRecognizer's state becomes UIGestureRecognizerStateEnded, use the velocityInView: message to find the velocity of the gesture.

如果速度接近于零,则打开或关闭视图基于视图的位置和视图的先前状态。例如,如果视图已关闭且已拉出超过10%,请将其打开。如果它是打开的并且已被拉超过10%,请关闭它。否则,将其移回到预手势位置。

If the velocity is close to zero, open or close the view based on the position of the view and the previous state of the view. For example, if the view was closed and it has been pulled out more than 10%, open it. If it was open and has been pulled in more than 10%, close it. Otherwise, move it back to its pre-gesture position.

如果速度不接近零,请使用Y分量的符号来确定新的状态。视图。如果符号为正,请关闭视图。如果符号为负数,请打开视图。

If the velocity is not close to zero, use the sign of the Y component to determine the new state of the view. If the sign is positive, close the view. If the sign is negative, open the view.

您必须尝试确切地确定接近零的定义最佳。

You will have to experiment to figure out exactly what definition of "close to zero" feels best.

在任何情况下,您都希望在手势结束后使用较短的持续时间(可能介于.1和.25秒之间)将视图设置为最终位置。您可能希望根据速度和视图需要移动的距离来选择持续时间。系统通知面板执行此操作。 (尝试慢慢地将其向下拖动到快速。它以不同的速度动画到最终位置,具体取决于你放开时拖动它的速度。)

In any case, you will want to animate the view to its final position after the gesture ends, using a short duration (probably between .1 and .25 seconds). You may want to choose the duration based on the velocity and the distance the view needs to travel. The system notifications panel does this. (Try dragging it partway down slowly vs. rapidly. It animates to its final position at different speeds depending on how fast you were dragging it when you let go.)

你我想试验找到最好的动画曲线( UIViewAnimationOptionCurveEaseOut 等),你可能想要根据你是打开还是关闭视图来使用不同的曲线和手势的速度。

You will want to experiment to find the best animation curve (UIViewAnimationOptionCurveEaseOut, etc.), and you may want to use a different curve depending on whether you're opening or closing the view and the velocity of the gesture.

这篇关于拖动UIView部分方式,然后它自己移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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