1

我有MapView .. 我将此添加为 ViewController 的视图的子视图。我在 ViewDidLoad 中有以下代码:

[self.view addSubview:mapView];
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(mapLongPress:)]; 
    longPressGesture.minimumPressDuration = 2; 
    [mapView addGestureRecognizer:longPressGesture];
    [longPressGesture release];

和 ,

- (void)mapLongPress:(UILongPressGestureRecognizer *)gestureRecognizer{
    NSLog(@"Gesture");
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
        CGPoint touchLocation = [gestureRecognizer locationInView:mapView];

        CLLocationCoordinate2D coordinate;
        coordinate = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];

这些是我从 StackOverFlow 获得的……但它不起作用……我还需要在这方面做些什么吗?

4

3 回答 3

1

尝试添加[self.view addSubview:mapView];[mapView addGestureRecognizer:longPressGesture];

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(mapLongPress:)]; 
longPressGesture.minimumPressDuration = 2; 
longPressGesture.delegate = self;
[self.mapView addGestureRecognizer:longPressGesture];
[self.view addSubview:mapView];
[longPressGesture release];
于 2012-09-05T11:30:23.273 回答
1

只需将UIGestureRecognizerDelegate添加到 ViewController 并执行我给出的上述代码!

于 2012-09-05T12:04:55.040 回答
0

试试这个:

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                      initWithTarget:self action:@selector(mapLongPress:)];
lpgr.minimumPressDuration = 2.0;

[self.mapView addGestureRecognizer:lpgr];

这是使用 ARC,所以我不确定你现在是否应该释放手势。尝试不释放它,然后尝试释放它。看看会不会影响手势。

于 2012-09-05T12:09:03.163 回答