简体   繁体   English

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

[英]Detect user touches on MKMapView in iOS 5

I have a MKMapView in a ViewController and would like to detect users' gestures when he/she touches the map with these methods: 我在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;

The app works fine with iOS 3, iOS 4 but when I debug the app with iPhone running on iOS 5, I see this message: 该应用程序适用于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>>

and the code in the above 4 methods are not reached. 并且未达到上述4种方法中的代码。

Do you know how to fix it? 你知道怎么解决吗?

Thanks. 谢谢。

Some form of UIGestureRecognizer can help you out. 某种形式的UIGestureRecognizer可以帮助你。 Here's an example of a tap recognizer being used on a map view; 这是在地图视图中使用的点击识别器的示例; let me know if this isn't what you're looking for. 如果这不是你想要的,请告诉我。

// 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM