在iOS 5中检测用户对MKMapView的触摸

时间:2011-09-22 21:15:57

标签: iphone objective-c ios mkmapview touch-event

我在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 个答案:

答案 0 :(得分: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;
    }
}