2014-11-13 2 views
2

Я пытаюсь добавить Pin на моей карте, когда пользователь делает longPress (с UILongPressGestureRecognizer), но это не работает. Однако большая проблема заключается в следующем: карта больше не отображается. Почему карта исчезла?MapView больше не работает, если я добавляю GestureRecognizer

Вы найдете проект-файл here

--------------------------- ViewController.swift ----- ---------------------------

{ 

import UIKit 
import MapKit 

class ViewController: UIViewController, MKMapViewDelegate { 

@IBOutlet var theMapView: MKMapView! 
@IBOutlet var theTextfield: UITextField! 

@IBOutlet var theLabel: UILabel! 

@IBAction func theButton(sender: UIButton) { 
    theLabel.text = "Swift-App schreibt \(theTextfield.text)" 
    theTextfield.resignFirstResponder() 
} 


override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    // Position 
    var latitude:CLLocationDegrees = 48.399193 
    var longitude:CLLocationDegrees = 9.993341 
    // Zoomfaktor 
    var latDelta:CLLocationDegrees = 0.01 
    var longDelta:CLLocationDegrees = 0.01 
    // 
    var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta,longDelta) 



    // Koordinaten der Kirche 
    var churchLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude) 
    // Zentrum und Kartenausschnitt 
    var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(churchLocation, theSpan) 


    // LongTap definieren 
    let longpress = UILongPressGestureRecognizer(target: theMapView, action: "actionPin:") 
    longpress.minimumPressDuration = 1.0 
    longpress.numberOfTouchesRequired = 1 
    longpress.allowableMovement = 100 
    theMapView.addGestureRecognizer(longpress) 


    // Karte anzeigen 
    theMapView.setRegion(theRegion,animated:false) 

    // Pin setzen 
    var theUlmMinsterAnnotation = MKPointAnnotation() 
    theUlmMinsterAnnotation.coordinate = churchLocation 
    theUlmMinsterAnnotation.title = "Ulmer Münster" 
    theUlmMinsterAnnotation.subtitle = " Untertitel" 

    theMapView.addAnnotation(theUlmMinsterAnnotation) 
} 

func actionPin(gestureRecognizer:UIGestureRecognizer) { 
    var touchpoint = gestureRecognizer.locationInView(self.theMapView) 
    var newCoord:CLLocationCoordinate2D=theMapView.convertPoint(touchpoint, toCoordinateFromView: self.theMapView) 
    var newAnnotation = MKPointAnnotation() 
    newAnnotation.coordinate = newCoord 
    newAnnotation.title = "Fingertipp" 
    newAnnotation.subtitle = "Untertitel" 
    theMapView.addAnnotation(newAnnotation) 
} 

}} 
+0

Если бы вы могли опубликовать соответствующий код, вы можете получить лучший ответ. То, что вы делаете, звучит не так сложно, что оно не может вписаться в вопрос. – Acey

+0

@Acey сделано. благодаря! – Rico30

+0

Вы используете раскадровку/xib, чтобы добавить 'theMapView' в ваш контроллер? – thelaws

ответ

0

Поскольку ваш размер карты равен {0,0}, и вы не используете какой-либо сдерживать.

Проверить здесь проект с незначительным обновлением https://dl.dropboxusercontent.com/u/19438780/test1-v2.zip

+0

Спасибо, теперь я вижу карту. препятствия, по-видимому, являются важной темой для изучения чего-то. :) Но у вас возникли какие-либо идеи, почему long-tap не распознается? – Rico30

+0

Не так просто добавить длинный щелчок на MKMapView, потому что есть еще один UIGestureRecognizer ... одно решение может быть .. подклассификация MKMapView http://stackoverflow.com/a/21124351/1702413 ... или попытка «обнаружить» эти существующие жесты и requireGestureRecognizerToFail для всех – TonyMkenu

Смежные вопросы