简体   繁体   English

使用UILongPressGestureRecognizer删除MKA注释

[英]Using UILongPressGestureRecognizer to Remove MKAnnotation

I can't seem to get the long press gesture recognizer to work on the annotation view, only the map view. 我似乎无法让长按手势识别器在注释视图上工作,仅在地图视图上工作。 The abridged version of my code is: 我的代码的简化版本是:

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate, MKMapViewDelegate, UIGestureRecognizerDelegate, UIActionSheetDelegate>
{
    CLLocationManager *locationManager;

    // Views
    IBOutlet MKMapView *mapView;
    IBOutlet MKAnnotationView *annotationView;

    UILongPressGestureRecognizer *longPress;
}

and parts of the implementation in AppDelegate.m ... 以及AppDelegate.m中的部分实现...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];

    [mapView setShowsUserLocation:YES];

    longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self 
                                                         action:@selector(deleteSelectedAnnotation:)];

    [[self window] makeKeyAndVisible];
    return YES;
}

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views
{
    annotationView = [views objectAtIndex:0];
    [annotationView addGestureRecognizer:longPress];
    id <MKAnnotation> mp = [annotationView annotation];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 250, 250);
    [mv setRegion:region animated:YES];
}

- (IBAction)deleteSelectedAnnotation:(UIGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"long press");
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Delete Record?" 
                                                                 delegate:self 
                                                        cancelButtonTitle:nil 
                                                   destructiveButtonTitle:nil 
                                                        otherButtonTitles:@"Yes",@"No",nil];
                                      [actionSheet showInView:mapView];
                                      [actionSheet release];
    }
}

So this whole thing works if i change the view object in line [annotationView addGestureRecognizer:longPress] to mapView . 因此,如果我将[annotationView addGestureRecognizer:longPress]行中的视图对象更改为mapView则整个工作正常。 But I only want the action to occur if the user is pressing the annotation, not anywhere on the map. 但是我只希望操作在用户按下注释时发生,而不是在地图上的任何地方。 What am I doing wrong? 我究竟做错了什么?

Thanks. 谢谢。

You can check if the touch is inside the annotation view's bounds: 您可以检查触摸是否在注释视图的范围内:

UIView *annotationView = [mapView viewForAnnotation:myAnnotation];
if ( annotationView && CGRectContainsPoint(annotationView.bounds, [sender locationInView:annotationView]) )
{
    // we can continue
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM