Я не могу заставить распознаватель жестов при длинных нажатиях работать с видом аннотации, только с видом карты.Сокращенная версия моего кода:
@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]
до mapView
.Но я хочу, чтобы действие происходило только в том случае, если пользователь нажимает аннотацию, а не где-либо на карте.Что я делаю не так?
Спасибо.