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...
}
}
}
}
पूरी तरह से काम करता है! – JimmyJammed
यह आईओएस 7 पर काम नहीं करता है, एक एमकेन्यूएनोटेशनकंटनर व्यू हिट टेस्ट में वापस आ गया है। इसे ठीक करने के बारे में कोई विचार? –
@RodrigoRuiz, MKNewAnnotationContainerView (निजी श्रेणी) जब आप मानचित्र क्षेत्र पर टैप करते हैं तो यह वापस किया जाता है, जो इस विशेष कोड को परवाह नहीं करता है। जब आप एनोटेशन पर टैप करते हैं, तो आईओएस 7 अभी भी एमकेएनोटेशन व्यू (डॉक्यूमेंटेड क्लास) लौटाता है जो कोड _is के लिए जाँच कर रहा है। यदि अभी भी कोई समस्या है, तो कृपया विवरण के साथ एक नया प्रश्न शुरू करें। – Anna