LoginSignup
3
0

More than 5 years have passed since last update.

マップ上の同じピンを連続でタップした際に無反応だった時の対処法

Posted at

MKMapViewの標準calloutの代わりに、独自Viewを使った際の挙動でちょっと詰まったのでメモします。

事象

マップ上の同じピンを2回連続でタップしても独自Viewが呼び出せない。

内容

MKMapviewのannotation(ピン)をタップした時にcallout(吹き出し)を使わずに独自のビューを表示する場合、calloutを表示しないようcanCalloutfalseを設定して、MKMapViewDelegateのdidSelectでViewの呼び出しを行うかと思います。

calloutを呼び出さないようにする
    func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
        for view in views {
            view.canShowCallout = false
        }
    }
ピンをタップして独自Viewを呼び出す処理(今回はViewControllerを表示してます)
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
        let modalViewController = ViewController()
        modalViewController.delegate = self                    
        self.present(modalViewController, animated: false, completion: nil)
    }

modalViewControllerから呼び出し元(MKMapViewが表示されているViewController)に戻り、再度同じピンをタップしてもmodalViewControllerは開かれませんでした(didSelectも呼ばれていない)。

対処法

canShowCalloutfalseでもピンをタップした時にcalloutが開かれたような状態になっているようなので、同じピンへのdisSelectが効かなかったように思われました。
ですので、ピンの選択を解除する処理をdidSelect内に入れたところ、独自View表示と同時にピンの選択(calloutの表示)も解除されるため、同じピンへの2回連続タップも効くようになりました。

didSelectにピンの選択解除処理を入れる
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
        let modalViewController = ViewController()
        modalViewController.delegate = self                    
        self.present(modalViewController, animated: false, completion: nil)

        // ピンの選択を解除
        for annotaion in mapView.selectedAnnotations {
            mapView.deselectAnnotation(annotaion, animated: false)
        }
    }

ちなみに setSelected でも良さそうでしたが、こちらは効果がありませんでした。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0