iPhone: Detecting Tap in MKMapView

后端 未结 8 1442
天命终不由人
天命终不由人 2020-12-24 07:35

How do I detect a single tap on an instance of MKMapView? Do I have to subclass MKMapView and then override the touchesEnded method?

8条回答
  •  臣服心动
    2020-12-24 07:55

    Nothing I ever found worked, but I came up with this unperfect solution : In viewDidLoad

    let singleTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onMapClicked))
    singleTapRecognizer.delegate = self
    mapView.addGestureRecognizer(singleTapRecognizer)
    

    In delegate :

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return touch.view!.frame.equalTo(mapView.frame)
    }
    

提交回复
热议问题