2017-02-13 53 views
-1

我想設置我的MKMapView,這樣當我按住地圖2秒鐘時,用戶當前位置就會出現一個別針。Swift 3 MKMapView - 在長按時添加pin /註釋

以下是我在我的viewDidLoad

let gestureRecognizer = UILongPressGestureRecognizer(target: self, action:(Selector(("longPress:")))) 
gestureRecognizer.minimumPressDuration = 2.0 
gestureRecognizer.delegate = self 
map.addGestureRecognizer(gestureRecognizer) 
在我的ViewController類的底部

然後,我有以下幾點:

func longPress(gestureRecognizer: UILongPressGestureRecognizer) { 

    let coordinate = map.centerCoordinate 

    let annotation = MKPointAnnotation() 
    annotation.coordinate = coordinate 
    map.addAnnotation(annotation) 
} 

當我跑地圖上的應用程序,並按下2秒鐘,應用程序崩潰。控制檯說

型NSException的未捕獲的異常


無法識別的選擇發送到實例0x7f89f0c02ad0

結束,我不知道爲什麼。感謝您提供任何指導。

+0

http://stackoverflow.com/questions/24681275/how-to-do-a-long-press-in-swift – 2017-02-13 00:16:23

回答

0

不要說

Selector(("longPress:")) 

#selector(longPress) 
+0

簡單的解決方案,完全解決了我的問題。謝謝! –

+0

我喜歡這種情況。 :)認真使用'#selector'語法;它的目的是防止_exactly_你得到的錯誤。 – matt