MapKitとSwiftUIを用いて、
①長押しした際にピンを立てる。
②そのピンから吹き出し(UIbutton付き)を出す。
③UIbuttonをタッチした際、別のViewを表示する。
ということをしたく、試しにprintで、適当な文字を表示しようと思い、
下記のようにコードを書いたのですが上手くいきませんでした。
②までのUIbutton付きの吹き出しを出すことはできたのですが、ボタンを押しても何の挙動も得られない状態です。
※できればボタンを複数個作成し、それぞれ別の挙動をさせたいです。
何も起きない原因と、解決方法をご教授いただきたいです。
よろしくお願いいたします。
swift
1 2import SwiftUI 3import MapKit 4 5struct MapView: UIViewRepresentable { 6 7 func makeUIView(context: Context) -> MKMapView { 8 let map = MKMapView() 9 map.delegate = context.coordinator 10 map.addGestureRecognizer(context.coordinator.myLongPress) 11 return map 12 } 13 14 func makeCoordinator() -> Coordinator { 15 Coordinator(self) 16 } 17 18 func updateUIView(_ mapView: MKMapView, context: Context) { 19 let latitude = 43.31513, longitude = 143.08874 20 let coordinate = CLLocationCoordinate2DMake(latitude, longitude) 21 // 縮尺を設定 22 let span = MKCoordinateSpan(latitudeDelta: 5, longitudeDelta: 5) 23 // マップの中心を設定 24 let region = MKCoordinateRegion(center: coordinate, span: span) 25 mapView.setRegion(region, animated: true) 26 } 27 28 class Coordinator: NSObject, MKMapViewDelegate { 29 var parent: MapView 30 let myLongPress: UILongPressGestureRecognizer = UILongPressGestureRecognizer() 31 32 init(_ parent: MapView) { 33 self.parent = parent 34 super.init() 35 self.myLongPress.addTarget(self, action: #selector(recognizeLongPress)) 36 } 37 38 @objc func recognizeLongPress(sender: UILongPressGestureRecognizer) { 39 if sender.state == .ended { 40 if let mapView = sender.view as? MKMapView { 41 // タップした位置を取得 42 let point = sender.location(in: mapView) 43 // mapView上での位置に変換 44 let coordinate = mapView.convert(point, toCoordinateFrom: mapView) 45 // アノテーション作成 46 let annotation = MKPointAnnotation() 47 annotation.coordinate = coordinate 48 annotation.title = "タイトル" 49 mapView.addAnnotation(annotation) 50 } 51 } 52 } 53 54 //↓ボタン(btn)を押しても呼ばれない。 55 func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 56 print("1234567890")//とりあえず表示出来るか試し 57 //その後は他のViewへ遷移もしたい 58 } 59 60 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 61 let identifier = "annotation" 62 if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) { 63 annotationView.annotation = annotation 64 return annotationView 65 } else { 66 let annotationView = MKAnnotationView( 67 annotation: annotation, 68 reuseIdentifier: identifier 69 ) 70 71 //stackViewの設定 72 let stackView = UIStackView() 73 stackView.axis = NSLayoutConstraint.Axis.vertical 74 stackView.alignment = UIStackView.Alignment.leading 75 76 //景色の画像の作成 77 let imageView = UIImageView(image: UIImage(named: "Try")) 78 imageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50) 79 imageView.contentMode = .scaleAspectFit 80 stackView.addArrangedSubview(imageView) 81 82 //ボタンの作成 83 let btn = UIButton(type: .custom) 84 btn.frame = CGRect(x: 0, y: 0, width: 20, height: 20) 85 btn.setImage(UIImage(named: "Hensyu"), for: .normal) 86 stackView.addArrangedSubview(btn) 87 88 annotationView.image = UIImage(named: "Eat") 89 annotationView.canShowCallout = true 90 annotationView.detailCalloutAccessoryView = stackView 91 annotationView.isUserInteractionEnabled = true 92 return annotationView 93 } 94 } 95 } 96} 97
回答1件
あなたの回答
tips
プレビュー