带 LongPressGestureRecognizer 的 Mapkit 间歇性工作
Mapkit with LongPressGestureRecognizer working intermittently
我有一个非常简单的设置,我有一个 MapKit,我希望每次用户在地图上长按时都能将图钉放到它上面。
它适用于第一个引脚。但是,如果用户尝试再次长按(在另一个位置),则无法识别该手势。第三次成功,第四次失败,依此类推(每一次奇怪的尝试都会被识别)。
我有一个示例项目可以证明这一点:
https://github.com/alexwibowo/SimpleLocationMarker
基本上,我的视图控制器如下所示:
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView! {
didSet{
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.addNewAnnotation(recognizer:)))
gestureRecognizer.minimumPressDuration = 0.5
mapView.addGestureRecognizer(gestureRecognizer)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
@objc func addNewAnnotation(recognizer: UILongPressGestureRecognizer){
if (recognizer.state != .began) {
return
}
let touchPoint = recognizer.location(in: mapView)
let wayCoords = mapView.convert(touchPoint, toCoordinateFrom: mapView)
let annotation = MKPointAnnotation()
annotation.title = "New"
annotation.coordinate = wayCoords
mapView.addAnnotation(annotation)
}
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "locationMarkerIdentifier"
var view: MKPinAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? MKPinAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.annotation = annotation
view.canShowCallout = true
view.isDraggable = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
return view
}
}
我确定这是我犯的一个非常简单的错误!
首先,将其委托设置为自己。
@IBOutlet weak var mapView: MKMapView! {
didSet{
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.addNewAnnotation(recognizer:)))
gestureRecognizer.minimumPressDuration = 0.5
mapView.addGestureRecognizer(gestureRecognizer)
gestureRecognizer.delegate = self
}
}
第二,
extension ViewController : UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
我有一个非常简单的设置,我有一个 MapKit,我希望每次用户在地图上长按时都能将图钉放到它上面。 它适用于第一个引脚。但是,如果用户尝试再次长按(在另一个位置),则无法识别该手势。第三次成功,第四次失败,依此类推(每一次奇怪的尝试都会被识别)。
我有一个示例项目可以证明这一点: https://github.com/alexwibowo/SimpleLocationMarker
基本上,我的视图控制器如下所示:
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView! {
didSet{
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.addNewAnnotation(recognizer:)))
gestureRecognizer.minimumPressDuration = 0.5
mapView.addGestureRecognizer(gestureRecognizer)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
@objc func addNewAnnotation(recognizer: UILongPressGestureRecognizer){
if (recognizer.state != .began) {
return
}
let touchPoint = recognizer.location(in: mapView)
let wayCoords = mapView.convert(touchPoint, toCoordinateFrom: mapView)
let annotation = MKPointAnnotation()
annotation.title = "New"
annotation.coordinate = wayCoords
mapView.addAnnotation(annotation)
}
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "locationMarkerIdentifier"
var view: MKPinAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? MKPinAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.annotation = annotation
view.canShowCallout = true
view.isDraggable = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
return view
}
}
我确定这是我犯的一个非常简单的错误!
首先,将其委托设置为自己。
@IBOutlet weak var mapView: MKMapView! {
didSet{
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.addNewAnnotation(recognizer:)))
gestureRecognizer.minimumPressDuration = 0.5
mapView.addGestureRecognizer(gestureRecognizer)
gestureRecognizer.delegate = self
}
}
第二,
extension ViewController : UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}