使用 UILongPressGestureRecognizer 删除 MKAnnotation
我似乎无法让长按手势识别器在注释视图上工作,只能在地图视图上工作。我的代码的删节版本是:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以检查触摸是否在注释视图的边界内:
You can check if the touch is inside the annotation view's bounds: