有没有办法确定是否拖动了MKMapView?
每当用户使用拖动地图时,我都希望获得中心位置,CLLocationCoordinate2D centre = [locationMap centerCoordinate];
但是我需要一个委托方法或在用户浏览地图时立即触发的东西。
提前致谢
Answers:
查看MKMapViewDelegate参考。
具体来说,这些方法可能有用:
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
确保设置了地图视图的委托属性,以便调用这些方法。
由于任何原因更改区域时,将触发接受的答案中的代码。要正确检测地图拖动,您必须添加UIPanGestureRecognizer。顺便说一句,这是拖动手势识别器(平移=拖动)。
步骤1:在viewDidLoad中添加手势识别器:
-(void) viewDidLoad {
[super viewDidLoad];
UIPanGestureRecognizer* panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didDragMap:)];
[panRec setDelegate:self];
[self.mapView addGestureRecognizer:panRec];
}
步骤2:将协议UIGestureRecognizerDelegate添加到视图控制器,以便它可以充当委托。
@interface MapVC : UIViewController <UIGestureRecognizerDelegate, ...>
步骤3:并为UIPanGestureRecognizer添加以下代码,以使其与MKMapView中已经存在的手势识别器一起使用:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
第4步:如果您要一次调用方法而不是每次拖动50次,请在选择器中检测到“拖动结束”状态:
- (void)didDragMap:(UIGestureRecognizer*)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
NSLog(@"drag ended");
}
}
UIPinchGestureRecognizer
,以及
readyForUpdate
,然后在中检查该标志- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
。
这是检测用户发起的平移和缩放更改的唯一方法:
- (BOOL)mapViewRegionDidChangeFromUserInteraction
{
UIView *view = self.mapView.subviews.firstObject;
// Look through gesture recognizers to determine whether this region change is from user interaction
for(UIGestureRecognizer *recognizer in view.gestureRecognizers) {
if(recognizer.state == UIGestureRecognizerStateBegan || recognizer.state == UIGestureRecognizerStateEnded) {
return YES;
}
}
return NO;
}
static BOOL mapChangedFromUserInteraction = NO;
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
mapChangedFromUserInteraction = [self mapViewRegionDidChangeFromUserInteraction];
if (mapChangedFromUserInteraction) {
// user changed map region
}
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
if (mapChangedFromUserInteraction) {
// user changed map region
}
}
MKMapView
iOS中的内部实现。该实现可以在任何iOS更新中更改,因为它不是API的一部分。
(仅)@mobi出色解决方案的Swift版本:
private var mapChangedFromUserInteraction = false
private func mapViewRegionDidChangeFromUserInteraction() -> Bool {
let view = self.mapView.subviews[0]
// Look through gesture recognizers to determine whether this region change is from user interaction
if let gestureRecognizers = view.gestureRecognizers {
for recognizer in gestureRecognizers {
if( recognizer.state == UIGestureRecognizerState.Began || recognizer.state == UIGestureRecognizerState.Ended ) {
return true
}
}
}
return false
}
func mapView(mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
mapChangedFromUserInteraction = mapViewRegionDidChangeFromUserInteraction()
if (mapChangedFromUserInteraction) {
// user changed map region
}
}
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if (mapChangedFromUserInteraction) {
// user changed map region
}
}
self.mapView.subviews[0]
为self.mapView.subviews[0] as! UIView
self.map.delegate = self
Swift 3解决上述Jano问题的方法:
将协议UIGestureRecognizerDelegate添加到您的ViewController
class MyViewController: UIViewController, UIGestureRecognizerDelegate
在中创建UIPanGestureRecognizerviewDidLoad
并将其设置delegate
为self
viewDidLoad() {
// add pan gesture to detect when the map moves
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.didDragMap(_:)))
// make your class the delegate of the pan gesture
panGesture.delegate = self
// add the gesture to the mapView
mapView.addGestureRecognizer(panGesture)
}
添加一个Protocol方法,以便您的手势识别器可以使用现有的MKMapView手势
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
添加选择器将在平移手势中调用的方法
func didDragMap(_ sender: UIGestureRecognizer) {
if sender.state == .ended {
// do something here
}
}
根据我的经验,类似于“键入时搜索”,我发现计时器是最可靠的解决方案。它消除了为平移,捏合,旋转,敲击,双击等添加其他手势识别器的需求。
解决方案很简单:
当计时器触发时,为新区域加载标记
import MapKit
class MyViewController: MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
var mapRegionTimer: NSTimer?
// MARK: MapView delegate
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
setMapRegionTimer()
}
func setMapRegionTimer() {
mapRegionTimer?.invalidate()
// Configure delay as bet fits your application
mapRegionTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "mapRegionTimerFired:", userInfo: nil, repeats: false)
}
func mapRegionTimerFired(sender: AnyObject) {
// Load markers for current region:
// mapView.centerCoordinate or mapView.region
}
}
另一个可能的解决方案是在保存地图视图的视图控制器中实现touchesMoved:(或touchesEnded:等),如下所示:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
for (UITouch * touch in touches) {
CGPoint loc = [touch locationInView:self.mapView];
if ([self.mapView pointInside:loc withEvent:event]) {
#do whatever you need to do
break;
}
}
}
在某些情况下,这可能比使用手势识别器更简单。
您还可以在Interface Builder中将手势识别器添加到地图中。将其链接到其在viewController中的动作的插座,我将其称为“ mapDrag” ...
然后,您将在viewController的.m中执行以下操作:
- (IBAction)mapDrag:(UIPanGestureRecognizer *)sender {
if(sender.state == UIGestureRecognizerStateBegan){
NSLog(@"drag started");
}
}
确保那里也有这个:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
当然,您必须在.h文件中将viewController设置为UIGestureRecognizerDelegate才能起作用。
否则,地图的响应者是唯一听到手势事件的人。
UIGestureRecognizerStateBegan
要识别任何手势在地图视图上何时结束:
这对于仅在用户完成缩放/旋转/拖动地图之后仅执行数据库查询非常有用。
对我来说,regionDidChangeAnimated方法仅在完成手势后才被调用,并且在拖动/缩放/旋转时并没有被多次调用,但是了解是否是由于手势引起的很有用。
这些解决方案中有很多都存在问题,而不是Swift想要的,所以我选择了一个更干净的解决方案。
我只是将MKMapView子类化,并覆盖touchesMoved。尽管此代码段不包含此代码段,但我建议创建一个委托或通知以传递您想要的有关该运动的任何信息。
import MapKit
class MapView: MKMapView {
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
print("Something moved")
}
}
您将需要更新情节提要文件上的类以指向此子类,以及修改通过其他方式创建的任何地图。
如评论中所述,Apple不鼓励使用子类化MKMapView
。尽管这取决于开发人员的判断,但这种特殊用法不会更改地图的行为,并且已经为我工作了三年多,没有发生任何事件。但是,过去的性能并不表示将来的兼容性,因此请警告。
Jano的答案对我有用,所以我认为我会留下Swift 4 / XCode 9的更新版本,因为我不太精通Objective C,而且我敢肯定还有其他一些都不是。
步骤1:在viewDidLoad中添加以下代码:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(didDragMap(_:)))
panGesture.delegate = self
步骤2:确保您的类符合UIGestureRecognizerDelegate:
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UIGestureRecognizerDelegate {
步骤3:添加以下功能,以确保您的panGesture与其他手势同时使用:
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
步骤4:并确保您的方法不被称为“每次拖动50次”,正如Jano正确指出的那样:
@objc func didDragMap(_ gestureRecognizer: UIPanGestureRecognizer) {
if (gestureRecognizer.state == UIGestureRecognizerState.ended) {
redoSearchButton.isHidden = false
resetLocationButton.isHidden = false
}
}
*请注意在最后一步中添加了@objc。XCode将在您的函数上强制使用此前缀,以便对其进行编译。
我知道这是一篇旧文章,但在这里是我的Jano的Swift 4/5代码,它回答了Pan和Pinch手势。
class MapViewController: UIViewController, MapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.didDragMap(_:)))
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.didPinchMap(_:)))
panGesture.delegate = self
pinchGesture.delegate = self
mapView.addGestureRecognizer(panGesture)
mapView.addGestureRecognizer(pinchGesture)
}
}
extension MapViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
@objc func didDragMap(_ sender: UIGestureRecognizer) {
if sender.state == .ended {
//code here
}
}
@objc func didPinchMap(_ sender: UIGestureRecognizer) {
if sender.state == .ended {
//code here
}
}
}
请享用!
我试图在地图的中心添加一个注释,无论其用途如何,该注释始终位于地图的中心。我尝试了上述几种方法,但都不够好。最终,我找到了一种非常简单的解决方法,该方法借鉴了Anna的答案,并与Eneko的答案相结合。它基本上将regionWillChangeAnimated视为拖动的开始,将regionDidChangeAnimated视为拖动的结束,并使用计时器实时更新销钉:
var mapRegionTimer: Timer?
public func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
mapRegionTimer?.invalidate()
mapRegionTimer = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: { (t) in
self.myAnnotation.coordinate = CLLocationCoordinate2DMake(mapView.centerCoordinate.latitude, mapView.centerCoordinate.longitude);
self.myAnnotation.title = "Current location"
self.mapView.addAnnotation(self.myAnnotation)
})
}
public func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
mapRegionTimer?.invalidate()
}
我设法以最简单的方式实现了此目的,该方式可以处理与地图的所有交互(用1/2 / N的手指轻敲/双击/ N轻敲,用1/2 / N的手指轻摇,捏和旋转)
gesture recognizer
并添加到地图视图的容器gesture recognizer's
delegate
为一些对象实现UIGestureRecognizerDelegate
gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch)
方法
private func setupGestureRecognizers()
{
let gestureRecognizer = UITapGestureRecognizer(target: nil, action: nil)
gestureRecognizer.delegate = self
self.addGestureRecognizer(gestureRecognizer)
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool
{
self.delegate?.mapCollectionViewBackgroundTouched(self)
return false
}
首先,确保您当前的视图控制器是地图的委托。因此,将您的Map View委托设置为self并添加MKMapViewDelegate
到您的视图控制器中。下面的例子。
class Location_Popup_ViewController: UIViewController, MKMapViewDelegate {
// Your view controller stuff
}
并将其添加到您的地图视图
var myMapView: MKMapView = MKMapView()
myMapView.delegate = self
其次,添加此功能,该功能在地图移动时触发。它将滤除所有动画,并且仅在与之交互时才触发。
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if !animated {
// User must have dragged this, filters out all animations
// PUT YOUR CODE HERE
}
}
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
做好了