iPhone:如何在地图上将长按手势和拖动操作结合起来?
我已经尝试了很多事情,此时发布代码可能会令人困惑,所以让我从这个概念开始。
我需要以某种方式将长按和触摸拖动的操作组合成单个操作,例如 LongPressThenDragGestureRecognizer。我试图在 MKMapView 上完成此任务,所以我不能一直禁用用户交互,因为我想要地图的平移和缩放功能。
让事情变得有点复杂的是,用户长按以识别交互的初始项目(MKOverlay 对象)需要被删除并替换为新的绘制对象。那时,代码不再关心对象,只关心手指在任何给定点的位置(我将在拖动的对象移动时重新绘制它们)。
工作流程如下:
- 用户在地图上看到一个覆盖层
- 用户触摸并按住该项目,让应用程序知道他们想要拖动它
- 应用程序用绘制的对象替换覆盖层并禁用地图,使其无法启动平移(而不是拖动)。
- 用户拖动手指,对象在移动时重新绘制。
- 用户抬起手指完成拖动
- 应用程序用新的地图覆盖替换绘制的对象
- 应用程序允许用户在地图上进行交互以允许平移/缩放/注释选择等。
到目前为止,我已经尝试了很多方法,但收效甚微。下面列出了迄今为止我所取得的最佳结果。这是通过在 MKMapView 对象上使用 UILongPressGestureRecognizer(检查与叠加层的交集)来完成的,然后覆盖 TouchBegan 以进行地图触摸拖动。
- 显示覆盖图,并且用户成功执行了长按手势,该手势得到了适当的识别
- 图被绘制的对象替换
- 用户必须抬起手指并再次触摸才能启动拖动操作
- 地图用户交互被禁用,覆盖 用户抬起手指,绘制新的叠加层并再次启用地图交互
我已经很接近了,我只是不知道如何将手势组合成一个,这样用户就不必抬起手指并触摸再次向下开始拖动。
任何想法都将不胜感激。
I've tried a number of things and to post code at this point would likely be confusing, so let me start with the concept.
I need to somehow combine the operations of long press and touch drag into a single operation, something like LongPressThenDragGestureRecognizer. I'm trying to accomplish this on an MKMapView, so I can't just disable user interaction the entire time, because I want the pan and zoom functionality of the map.
To complicate things a bit, the initial item (an MKOverlay object) that the user long-presses on to recognize interaction will need to be removed and replaced with a new drawn object. At that point, the the code doesn't care about the object anymore, only where the finger is at any given point (I will redraw the dragged object as they move).
This is the workflow:
- User is presented an overlay on the map
- User touches and holds on the item to let the app know they want to drag it
- The app replaces the overlay with a drawn object and disables the map so that it doesn't start panning (rather than dragging).
- User drags finger, and object redraws as they move.
- User lifts finger to complete the drag
- App replaces the drawn object with a new map overlay
- App enables user interaction on the map to allow pan/zoom/annotation selection, etc.
I've tried a number of things so far, with little success. The best results I have so far is listed below. This was done using a UILongPressGestureRecognizer on the MKMapView objects (checking intersection with overlay), and then overriding touchesBegan for map touch drag.
- Overlay is shown and user successfully performs a long-press gesture that gets recognized appropriately
- Map user interaction is disabled and overlay is replaced with drawn object
- User has to lift finger and touch down again to initiate the drag operation
- When user lifts finger, new overlay is drawn and map interaction is enabled again
I'm so close, I just don't know how to combine the gestures into one, so that the user doesn't have to lift his/her finger and touch down again to start the drag.
Any ideas are greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这是一个像这样的复杂手势,我会很想完全避免使用手势识别器,并转移到 TouchesBegan、touchesMoved、touchesEnded、touchesCancelled 以及你移动的某种状态,因为手势碰巧知道你在哪里。
MKMapView 有一个 UIResponder 基类,因此应该很容易制作您自己的 MKMapView 派生版本,它响应触摸事件(记住将它们传递给超级地图以维持其正常功能)。
If it's a complex gesture like this, I would be tempted to avoid a gesture recognizer altogether and move to touchesBegan, touchesMoved, touchedEnded, touchesCancelled with some state that you move though as the gesture happens to know where you are.
MKMapView has a UIResponder base class so it should be easy to make your own derived version of MKMapView which responds to the touch events (remembering to passing them up to the super the map to maintain it's normal functionality).