用手指做一个UIView拖动

问题描述:

我有 mainViewController 但是我内部有一个小的 UIView ,当你点击 MKAnnotationView 在 MKMapKit 中,所以我需要 UIView 才可以拖放屏幕的任何部分。

I have the mainViewController but I have an small UIView inside that is activated when you tap a MKAnnotationView in the MKMapKit, so I need that UIView to be draggable in any part of the screen.

我的应用程序的示例屏幕截图:

An example screenshot of my app:

圆圈是一个例子,我想我可以拖动小UIView的任何一点。

The circle is an example of point, I suppose I can drag for any point of the "small" UIView.

我尝试使用 UITapGestureRecognizer 但它没有用我的代码不够好,我无法将其拖动,因为它只是点击,而不是点击和移动。

I tried using UITapGestureRecognizer but it didn't worked as my code was not good enough and I could not made it draggable because it's only the tap, not tap and move.

我希望你能帮助我。


  1. 使用 UIPanGestureRecognizer 而不是 UITapGestureRecognizer

  2. 为您的视图设置 userInteractionEnabled = YES

  3. 查看有关手势识别器的精彩教程:触摸。有一个很好的拖动视图的例子。

  1. Use UIPanGestureRecognizer instead of UITapGestureRecognizer
  2. Set userInteractionEnabled = YES for your view
  3. Review this nice tutorial about gesture recognizers: Touches. There is nice example of dragging views.