在iOS 5中检测用户在MKMapView上的触摸操作

6

我在一个ViewController中有一个MKMapView,希望能够使用以下方法检测用户的手势:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

该应用程序在iOS 3和iOS 4上运行良好,但是当我在运行iOS 5的iPhone上调试应用程序时,我看到了这条消息:

Pre-iOS 5.0 touch delivery method forwarding relied upon. Forwarding -touchesCancelled:withEvent: to <MKAnnotationContainerView: 0x634790; frame = (0 0; 262144 262144); autoresizesSubviews = NO; layer = <CALayer: 0x634710>>

以上4种方法中的代码没有被执行。

您知道如何解决这个问题吗?

谢谢。


1
目前无法对iOS 5发表评论,但对于3.2到4版本,使用UIGestureRecognizer可能比使用touches方法更容易。 - user467105
请查看此链接:https://dev59.com/z3NA5IYBdhLWcg3wNq8y - Kalpesh
1个回答

1

某种形式的UIGestureRecognizer可以帮助您解决问题。这里有一个在地图视图上使用轻拍识别器的示例;如果这不是您要找的,请告诉我。

// in viewDidLoad...

// Create map view
MKMapView *mapView = [[MKMapView alloc] initWithFrame:(CGRect){ CGPointZero, 200.f, 200.f }];
[self.view addSubview:mapView];
_mapView = mapView;

// Add tap recognizer, connect it to the view controller
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapViewTapped:)];
[mapView addGestureRecognizer:tapRecognizer];

// ...

// Handle touch event
- (void)mapViewTapped:(UITapGestureRecognizer *)recognizer
{
    CGPoint pointTappedInMapView = [recognizer locationInView:_mapView];
    CLLocationCoordinate2D geoCoordinatesTapped = [_mapView convertPoint:pointTappedInMapView toCoordinateFromView:_mapView];

    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
            /* equivalent to touchesBegan:withEvent: */
            break;

        case UIGestureRecognizerStateChanged:
            /* equivalent to touchesMoved:withEvent: */
            break;

        case UIGestureRecognizerStateEnded:
            /* equivalent to touchesEnded:withEvent: */
            break;

        case UIGestureRecognizerStateCancelled:
            /* equivalent to touchesCancelled:withEvent: */
            break;

        default:
            break;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接