Я пытаюсь добавить 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)
}
}}
Если бы вы могли опубликовать соответствующий код, вы можете получить лучший ответ. То, что вы делаете, звучит не так сложно, что оно не может вписаться в вопрос. – Acey
@Acey сделано. благодаря! – Rico30
Вы используете раскадровку/xib, чтобы добавить 'theMapView' в ваш контроллер? – thelaws