2010-11-30 9 views
30

पर टैप जेस्चर कैप्चर करने के लिए कैसे करें मैं अपने MKMapView पर टैप ईवेंट कैप्चर करने का प्रयास कर रहा हूं, इस तरह मैं उस बिंदु पर MKPinAnnotation छोड़ सकता हूं जहां उपयोगकर्ता टैप किया गया था। असल में मेरे पास MKOverlayViews (एक इमारत दिखाए जाने वाला एक ओवरले) के साथ ओवरले किया गया नक्शा है और मैं उस ओवरले के बारे में अधिक जानकारी देना चाहता हूं जब वे MKPinAnnotaion छोड़कर कॉलआउट में अधिक जानकारी दिखाकर उस पर टैप करते हैं। धन्यवाद।MKMapView

उत्तर

59

मानचित्र दृश्य पर छूने का पता लगाने के लिए आप UIGestureRecognizer का उपयोग कर सकते हैं।

एक ही टैप के बजाय, मैं एक डबल टैप (UITapGestureRecognizer) या एक लंबी प्रेस() की तलाश करने का सुझाव दूंगा। एक ही टैप पिन या कॉलआउट पर एकल टैप करने की कोशिश कर रहे उपयोगकर्ता के साथ हस्तक्षेप कर सकता है।

जगह है जहाँ आप सेटअप (उदाहरण के लिए viewDidLoad में) मानचित्र देखने के लिए, मानचित्र दृश्य के लिए इशारा पहचानकर्ता संलग्न में:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleGesture:)]; 
tgr.numberOfTapsRequired = 2; 
tgr.numberOfTouchesRequired = 1; 
[mapView addGestureRecognizer:tgr]; 
[tgr release]; 

या एक लंबे प्रेस का उपयोग करें:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleGesture:)]; 
lpgr.minimumPressDuration = 2.0; //user must press for 2 seconds 
[mapView addGestureRecognizer:lpgr]; 
[lpgr release]; 


handleGesture: विधि में:

- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer 
{ 
    if (gestureRecognizer.state != UIGestureRecognizerStateEnded) 
     return; 

    CGPoint touchPoint = [gestureRecognizer locationInView:mapView]; 
    CLLocationCoordinate2D touchMapCoordinate = 
     [mapView convertPoint:touchPoint toCoordinateFromView:mapView]; 

    MKPointAnnotation *pa = [[MKPointAnnotation alloc] init]; 
    pa.coordinate = touchMapCoordinate; 
    pa.title = @"Hello"; 
    [mapView addAnnotation:pa]; 
    [pa release]; 
} 
+0

सलाह के लिए धन्यवाद, एक बार जब मैं इसे काम कर रहा हूं तो मैं एप जवाब दूंगा। मैंने एकल टैप करने की कोशिश की लेकिन उसके बाद मैं अपने पिनएनोटेशन के कॉलआउट दिखाने में सक्षम नहीं था। ऐसा लगता है कि मुझे LongPressureGesture – sbkb

+4

का उपयोग करने की आवश्यकता है UITapGestureRecognizer अब आईओएस 6 में एमकेमैप व्यू पर नहीं पहचानता है। यह आईओएस 5 में काम करता है। इस समस्या पर कोई विचार? – Felix

+6

@ phix23, 'reRecognizeSimultaneouslyWithGestureRecognizer' को लागू करने का प्रयास करें और वहां से 'YES' वापस करें। उस shoud के लिए जीआर जोड़ने से पहले 'tgr.delegate = self;' करने की आवश्यकता होगी प्रतिनिधि विधि को पहचानने के लिए पहचानें। – Anna

5

मैंने viewDidLoad: में एक लंबी प्रेस (UILongPressGestureRecognizer) सेट की है लेकिन यह पहले से केवल एक स्पर्श का पता लगाती है।

मैं सभी स्पर्श का पता लगाने के लिए एक लंबी प्रेस कहां स्थापित कर सकता हूं? (इसका मतलब यह है कि प्रत्येक पिन को पिन को धक्का देने के लिए स्क्रीन पर उपयोगकर्ता का इंतजार करने का इंतजार है)

viewDidLoad: विधि!

- (void)viewDidLoad { 
    [super viewDidLoad];mapView.mapType = MKMapTypeStandard; 

    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)]; 
    [self.mapView addGestureRecognizer:longPressGesture]; 
    [longPressGesture release]; 

    mapAnnotations = [[NSMutableArray alloc] init]; 
    MyLocation *location = [[MyLocation alloc] init]; 
    [mapAnnotations addObject:location]; 

    [self gotoLocation]; 
    [self.mapView addAnnotations:self.mapAnnotations]; 
} 

और handleLongPressGesture विधि:

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender { 
    // This is important if you only want to receive one tap and hold event 
    if (sender.state == UIGestureRecognizerStateEnded) 
    {NSLog(@"Released!"); 
     [self.mapView removeGestureRecognizer:sender]; 
    } 
    else 
    { 
     // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map 
     CGPoint point = [sender locationInView:self.mapView]; 
     CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView]; 
     // Then all you have to do is create the annotation and add it to the map 
     MyLocation *dropPin = [[MyLocation alloc] init]; 
     dropPin.latitude = [NSNumber numberWithDouble:locCoord.latitude]; 
     dropPin.longitude = [NSNumber numberWithDouble:locCoord.longitude]; 
//  [self.mapView addAnnotation:dropPin]; 
     [mapAnnotations addObject:dropPin]; 
     [dropPin release]; 
     NSLog(@"Hold!!"); 
     NSLog(@"Count: %d", [mapAnnotations count]); 
    } 
} 
1

आप मानचित्र दृश्य में एक सिंगल क्लिक/नल का उपयोग करना चाहते हैं, तो यहाँ मैं उपयोग कर रहा हूँ कोड का एक टुकड़ा है। (कोको और स्विफ्ट)

let gr = NSClickGestureRecognizer(target: self, action: "createPoint:") 
gr.numberOfClicksRequired = 1 
gr.delaysPrimaryMouseButtonEvents = false // allows +/- button press 
gr.delegate = self 
map.addGestureRecognizer(gr) 
इशारा प्रतिनिधि विधि में

, एक साधारण परीक्षण डबल-टैप जेस्चर को पसंद करते हैं ...

func gestureRecognizer(gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: NSGestureRecognizer) -> Bool { 
    let other = otherGestureRecognizer as? NSClickGestureRecognizer 
    if (other?.numberOfClicksRequired > 1) { 
    return true; // allows double click 
    } 

    return false 
} 

आप भी अन्य प्रतिनिधि तरीकों में इशारा फ़िल्टर कर सकते हैं यदि आप चाहते थे नक्शा विभिन्न "राज्यों" में होना चाहिए, जिनमें से एक ने एक टैप/क्लिक