2011-09-22 89 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; 
    } 
}