사용자가 longPress (UILongPressGestureRecognizer 포함)를 수행 할 때 Pin을 추가하려고하는데 작동하지 않습니다. 그러나 더 큰 문제는 다음과 같습니다.지도가 더 이상 표시되지 않습니다. 왜지도가 사라 졌는가?GestureRecognizer를 추가하면 MapView가 더 이상 작동하지 않습니다.
당신은 프로젝트 파일을 찾을 수 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 done. 감사! – Rico30
스토리 보드/xib를 사용하여'theMapView'를 뷰 컨트롤러에 추가하고 있습니까? – thelaws