使用 UILongPressGestureRecognizer 删除 MKAnnotation

发布于 2024-12-22 17:25:02 字数 2242 浏览 1 评论 0原文

我似乎无法让长按手势识别器在注释视图上工作,只能在地图视图上工作。我的代码的删节版本是:

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

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

    UILongPressGestureRecognizer *longPress;
}

以及 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];
    }
}

因此,如果我将 [annotationView addGestureRecognizer:longPress] 行中的视图对象更改为 ,那么整个事情就会起作用地图视图。但我只希望当用户按下注释时才会发生该操作,而不是地图上的任何地方。我做错了什么?

谢谢。

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 ...

- (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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她如夕阳 2024-12-29 17:25:02

您可以检查触摸是否在注释视图的边界内:

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文