拖动手指下的 UIView
我想点击 UIView 并拖动,让该视图跟随我的手指,非常简单。但最简单的方法是将对象中心设置为点击发生的位置(这不是我想要的),我希望它移动,就好像您在点击对象的任何地方都抓住了对象一样。
有一种非常有用的方法可以做到这一点,并且在 iTunes U 视频之一中进行了参考。该脚本没有使用 deltaX、deltaY 将图像拖动到您点击的位置下方,而不是将其放在手指下方的中心,但我不记得该代码是什么!
有人可以参考这段代码吗?或者也许有一种在手指下移动 UIView 的有效方法,而无需 uiview.center = tap.center 概念?
I want to tap on a UIView and drag and have that view follow my finger, simple enough. But the easiest way of doing this is by setting the objects center to where the tap happened (Which is not what I want), I want it to move as if you grabbed the object wherever you tapped it.
There was a very useful way of doing this and it was reference in one of the iTunes U videos. The script didn't use deltaX, deltaY for dragging the image underneath where you tapped on it instead of having it center underneath your finger but I can't remember what that code was!
Does anyone have a reference to this code? Or perhaps have an efficient way of moving UIViews under a finger without having the uiview.center = tap.center concept?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下代码是允许面板/视图移动的简单手势识别器的示例。您不是修改中心,而是修改原点[基本上是通过为目标视图设置新框架]。
您可以根据您的情况对此进行优化,这样您就不必深入研究gesture.view...等
Swift 4:
The following code is an example of a simple gesture recognizer that will allow panel / view movement. Instead of modifying the center, you're modifying the origin point [basically by setting a new frame for the target view].
You can optimize this in your situation so you're not having to drill down into the gesture.view... etc
Swift 4:
这是来自 Apple MoveMe 项目的代码,关键是在
touchesMoved
方法中执行此操作。它允许UIView
(PlacardView
) 看到触摸并移动到用户触摸的位置。希望这有帮助。Here is the code from Apple's MoveMe project the key is to do this in the
touchesMoved
method. It allows theUIView
(PlacardView
) to see the touch and move wherever the user's touch goes. Hope this helps.我认为您正在谈论跟踪者应用程序...
I think you are talking about the stalker app...
您可以保存触摸点 (T) 和视图的当前位置 (O)。在touchMoved中,可以通过添加(OT)来基于新的点来移动它。
You can save both the point where you touched (T) and the current position of the view (O). In touchMoved, you can move it based on the new point by adding (O-T).