部分拖动 UIView,然后它自行移动
我有一个应用程序,其底部有一个可拖动的 UIView。可拖动视图并不完全离开屏幕,它有一个“拉动选项卡”,用户可以向上或向下拖动。目前可以上下拖动,但我想让它具有与 Apple 通知滑出抽屉相同的行为。
例如,如果我将视图向上拖动 50% 并将手指从屏幕上移开,那么我希望可拖动视图继续自行向上移动。同样,如果用户仅将视图拖出(例如向上 30%),则视图应回落到其默认位置。
理想情况下,虽然我可以向上/向下拖动,但动作不是很“有机”......
现在,我正在通过 UIPanGestureRecognizer 完成向上和向下拖动,以防万一这与问题相关。
是否可能是与可拖动视图的 Y 位置进行一些巧妙的数学运算,然后使用一些 CAAnimations 进行其余的移动?
可能有点难以想象,所以我在下面添加了一些屏幕。
谢谢 你!
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.
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"....
Right now, I'm accomplishing the dragging upwards and downwards via UIPanGestureRecognizer, just in case that's relevant to the question.
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.
Default screen with the a view at the bottom
The view dragged up via the tab on the right
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当
UIPanGestureRecognizer
的状态变为UIGestureRecognizerStateEnded
时,使用velocityInView:
消息查找手势的速度。如果速度接近于零,则根据视图的位置和视图的先前状态打开或关闭视图。例如,如果视图已关闭并且已拉出超过 10%,则将其打开。如果它是打开的并且已拉入超过 10%,请将其关闭。否则,将其移回到手势前的位置。
如果速度不接近于零,则使用 Y 分量的符号来确定视图的新状态。如果符号为正,则关闭视图。如果符号为负,则打开视图。
您必须进行试验才能准确地弄清楚“接近于零”的定义感觉最好。
在任何情况下,您都希望在手势结束后使用较短的持续时间(可能在 0.1 到 0.25 秒之间)将视图动画到其最终位置。您可能需要根据视图需要行进的速度和距离来选择持续时间。系统通知面板可以执行此操作。 (尝试缓慢向下拖动它与快速向下拖动它。它会以不同的速度动画到最终位置,具体取决于松开时拖动它的速度。)
您将需要尝试找到最佳的动画曲线(
UIViewAnimationOptionCurveEaseOut
等),并且您可能需要根据是否打开或关闭视图以及手势的速度使用不同的曲线。When your
UIPanGestureRecognizer
's state becomesUIGestureRecognizerStateEnded
, use thevelocityInView:
message to find the velocity of the gesture.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.
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.
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.)
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.