2011-09-22 51 views
6

我有一个MKMapView在一个视图控制器,并想检测用户的手势时,他/她倒是这些方法地图:检测用户触摸的iOS 5

- (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的 但是当我调试应用程序与iPhone上的iOS 5运行时,我看到这条消息:

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而不是触摸方法可能更容易。 – Anna

+0

http://stackoverflow.com/questions/1049889/how-to-intercept-touches-events-on-a-mkmapview-or-uiwebview-objects ..检查此链接 – Kalpesh

回答

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