2012-11-21 18 views
5

मेरे पास एक नक्शादृश्य है जो कुछ उपयोगकर्ता क्रियाओं के लिए त्रिज्या जानकारी प्रदर्शित करने के लिए MKCircle का उपयोग करता है।मानचित्र पर स्पर्श इशारा कैसे जोड़ें लेकिन पिन और एनोटेशन पर छूएं अनदेखा करें?

मैं जो करना चाहता हूं, वह उपयोगकर्ता को मानचित्र को छूने पर MKCircle को खारिज करने की अनुमति देता है। हालांकि, मैं MKCircle को खारिज नहीं करना चाहूंगा कि उपयोगकर्ता किसी भी अन्य पिन या MKCircle को स्वयं छूएं।

कोई विचार?

यहाँ मेरे वर्तमान कोड है, जो MKCircle जब नक्शा के किसी भी हिस्से को छुआ है खारिज है:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deactivateAllRadars)]; 
[tap setCancelsTouchesInView:NO]; 
[_mapView addGestureRecognizer:tap]; 

उत्तर

5

deactivateAllRadars विधि में, आप hitTest:withEvent: का उपयोग बताने के लिए है कि क्या एक MKAnnotationView उपयोग किया गया है या नहीं कर सकते।

इसका एक उदाहरण How can I catch tap on MapView and then pass it to default gesture recognizers? में दिखाया गया है (यह दूसरा कोड नमूना है)।

इससे आपको एनोटेशन टैप किया गया है, तो यह आपको मंडल को हटाने से बचने देगा।

एक एनोटेशन टैप नहीं किया गया है, तो आप देख सकते हैं कि एक MKCircle स्पर्श (उदाहरण के लिए How to capture Tap gesture on MKMapView देखें) के निर्देशांक हो रही है और देख रहा है, तो चक्र के केंद्र के लिए स्पर्श से दूरी से अधिक है के द्वारा उपयोग किया गया था इसका त्रिज्या

ध्यान दें कि deactivateAllRadars को deactivateAllRadars:(UITapGestureRecognizer *)tgr में बदला जाना चाहिए क्योंकि इसे संबंधित इशारा पहचानकर्ता से जानकारी की आवश्यकता होगी। विधि के चयनकर्ता के अंत में एक कोलन जोड़ने के लिए भी सुनिश्चित करें, जहां आप + init tap आवंटित करते हैं।

उदाहरण के लिए: तो तुम सीधे तरह तुलना नहीं कर सकते

func didTapOnMap(recognizer: UITapGestureRecognizer) { 
    let tapLocation = recognizer.locationInView(self) 
    if let subview = self.hitTest(tapLocation, withEvent: nil) { 
     if subview.isKindOfClass(NSClassFromString("MKNewAnnotationContainerView")!) { 
      print("Tapped out") 
     } 
    } 
} 

MKNewAnnotationContainerView, एक निजी भीतरी वर्ग है:

-(void)deactivateAllRadars:(UITapGestureRecognizer *)tgr 
{ 
    CGPoint p = [tgr locationInView:mapView]; 

    UIView *v = [mapView hitTest:p withEvent:nil]; 

    id<MKAnnotation> ann = nil; 

    if ([v isKindOfClass:[MKAnnotationView class]]) 
    { 
     //annotation view was tapped, select it... 
     ann = ((MKAnnotationView *)v).annotation; 
     [mapView selectAnnotation:ann animated:YES]; 
    } 
    else 
    { 
     //annotation view was not tapped, deselect if some ann is selected... 
     if (mapView.selectedAnnotations.count != 0) 
     { 
      ann = [mapView.selectedAnnotations objectAtIndex:0]; 
      [mapView deselectAnnotation:ann animated:YES]; 
     } 


     //remove circle overlay if it was not tapped...   
     if (mapView.overlays.count > 0) 
     { 
      CGPoint touchPoint = [tgr locationInView:mapView]; 

      CLLocationCoordinate2D touchMapCoordinate 
       = [mapView convertPoint:touchPoint toCoordinateFromView:mapView]; 

      CLLocation *touchLocation = [[CLLocation alloc] 
       initWithLatitude:touchMapCoordinate.latitude 
       longitude:touchMapCoordinate.longitude]; 

      CLLocation *circleLocation = [[CLLocation alloc] 
       initWithLatitude:circleCenterLatitude 
       longitude:circleCenterLongitude]; 

      CLLocationDistance distFromCircleCenter 
       = [touchLocation distanceFromLocation:circleLocation]; 

      if (distFromCircleCenter > circleRadius) 
      { 
       //tap was outside the circle, call removeOverlay... 
      } 
     } 
    } 
} 
+0

पूरी तरह से काम करता है! – JimmyJammed

+0

यह आईओएस 7 पर काम नहीं करता है, एक एमकेन्यूएनोटेशनकंटनर व्यू हिट टेस्ट में वापस आ गया है। इसे ठीक करने के बारे में कोई विचार? –

+0

@RodrigoRuiz, MKNewAnnotationContainerView (निजी श्रेणी) जब आप मानचित्र क्षेत्र पर टैप करते हैं तो यह वापस किया जाता है, जो इस विशेष कोड को परवाह नहीं करता है। जब आप एनोटेशन पर टैप करते हैं, तो आईओएस 7 अभी भी एमकेएनोटेशन व्यू (डॉक्यूमेंटेड क्लास) लौटाता है जो कोड _is के लिए जाँच कर रहा है। यदि अभी भी कोई समस्या है, तो कृपया विवरण के साथ एक नया प्रश्न शुरू करें। – Anna

3

यह मेरा स्विफ्ट 2.1 संगत संस्करण है

if subview is MKNewAnnotationContainerView { 
} 
+0

इस तरह से देर से, लेकिन जब आप "निजी आंतरिक वर्ग" कहते हैं, तो क्या इसका शाब्दिक अर्थ यह है कि यह एक निजी वर्ग है जो किसी अन्य वर्ग (शायद एमकेएनोटेशन व्यू) के अंदर है? – Dallas

+0

आप सही हैं, "भीतरी" भ्रामक है: मुझे नहीं पता कि वास्तव में MKNewAnnotationContainerView को परिभाषित किया गया है, लेकिन यह निश्चित रूप से निजी है। अगर आपको कोई दस्तावेज मिलता है, तो कृपया उत्तर अपडेट करने के लिए स्वतंत्र महसूस करें! –