アノテーションのボタンをタップしたら色が変わるようにしたいのですが、よくわからないことになっています。
0. mapView:viewFor に markerView.markerTintColor = .green を記述して色が変わる
0. mapView:viewFor で色が変えられるのだから色を変えるコードは間違っていない
0. ところが、mapView:calloutAccessoryControlTapped で同じコードを使うと色が変わらない
0. control == view.leftCalloutAccessoryView が間違っている?
0. 念の為 markerView.markerTintColor = .blue をelse{}に入れてみたら色が変わらない
0. ということは、markerView.markerTintColor は mapView:calloutAccessoryControlTapped では使えない
では、markerView.markerTintColor を使わずにアノテーションの色を変えるにはどうすればいいのでしょうか?
Swift
1markerView = MKMarkerAnnotationView() 2 3//選択したアノテーションにボタンを追加する 4 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 5 //アノテーションを緑にする ここは機能する ⭕ 6 markerView.markerTintColor = .green 7 } 8 9 10 //吹き出しアクササリー押下時の呼び出しメソッド 11 func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 12 if(control == view.leftCalloutAccessoryView) { 13 //機能しない ❌ 14 markerView.markerTintColor = .blue 15 } else { 16 //右のボタンが押された場合はピンを消す。 機能する⭕ 17 mapView.removeAnnotation(view.annotation!) 18 } 19 }
メソッドの全コードです。
Swift
1 markerView = MKMarkerAnnotationView() 2 3//選択したアノテーションにボタンを追加する 4 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 5 if annotation is MKUserLocation { 6 return nil 7 } 8 9 //マーカーアノテーションにコールアウトを表示 10 markerView.canShowCallout = true 11 let pinID = "pin" 12 var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinID) as? MKMarkerAnnotationView 13 if annotationView == nil { 14 annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: pinID) 15 //マーカー設置の時アニメーションをつける 16 markerView.animatesWhenAdded = true 17 //マーカーを緑にする 18 markerView.markerTintColor = .green//機能する⭕ 19 20 print("マーカーテストmarkerView:",markerView)//マーカーテストmarkerView: <MKMarkerAnnotationView: 0x7fb2ec016e00; frame = (121.802 248.686; 28 28); layer = <CALayer: 0x60000042ace0>> 21 22 //左ボタンをアノテーションビューに追加する。 23 let leftButton = UIButton() 24 leftButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40) 25 leftButton.setTitle("????", for: UIControlState.normal) 26 leftButton.setTitleColor(UIColor.black, for: UIControlState.normal) 27 markerView.detailCalloutAccessoryView = leftButton 28 29 //右ボタンをアノテーションビューに追加する。 30 let rightButton = UIButton() 31 rightButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40) 32 rightButton.setTitle("❌", for: UIControlState.normal) 33 rightButton.setTitleColor(UIColor.black, for: UIControlState.normal) 34 markerView.rightCalloutAccessoryView = rightButton 35 36 } else { 37 annotationView!.annotation = annotation 38 } 39 40 return markerView 41 } 42 43 //吹き出しアクササリー押下時の呼び出しメソッド 44 func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 45// 左ボタンが押された場合は青に変える 46 if(control == view.leftCalloutAccessoryView) { 47 markerView.markerTintColor = .blue//機能しない❌ 48 print("マーカーテスト2markerView:",markerView)//表示なし 49 } else { 50 //右のボタンが押された場合はピンを消す。 51 mapView.removeAnnotation(view.annotation!)//機能する⭕ 52 print("マーカーテス3トmarkerView:",markerView)//マーカーテスト3markerView: <MKMarkerAnnotationView: 0x7fb2ec016e00; frame = (121.802 248.686; 28 28); layer = <CALayer: 0x60000042ace0>> 53 } 54 }
回答2件
あなたの回答
tips
プレビュー