質問するログイン新規登録

質問編集履歴

5

コード追加

2018/06/01 07:43

投稿

clo.momo
clo.momo

スコア27

title CHANGED
File without changes
body CHANGED
@@ -84,7 +84,7 @@
84
84
  } else {
85
85
  //右のボタンが押された場合はピンを消す。
86
86
  mapView.removeAnnotation(view.annotation!)//機能する⭕
87
- print("マーカーテストmarkerView:",markerView)//マーカーテスト3markerView: <MKMarkerAnnotationView: 0x7fb2ec016e00; frame = (121.802 248.686; 28 28); layer = <CALayer: 0x60000042ace0>>
87
+ print("マーカーテス3トmarkerView:",markerView)//マーカーテスト3markerView: <MKMarkerAnnotationView: 0x7fb2ec016e00; frame = (121.802 248.686; 28 28); layer = <CALayer: 0x60000042ace0>>
88
88
  }
89
89
  }
90
90
  ```

4

コード追加

2018/06/01 07:43

投稿

clo.momo
clo.momo

スコア27

title CHANGED
File without changes
body CHANGED
@@ -52,6 +52,8 @@
52
52
  //マーカーを緑にする
53
53
  markerView.markerTintColor = .green//機能する⭕
54
54
 
55
+ print("マーカーテストmarkerView:",markerView)//マーカーテストmarkerView: <MKMarkerAnnotationView: 0x7fb2ec016e00; frame = (121.802 248.686; 28 28); layer = <CALayer: 0x60000042ace0>>
56
+
55
57
  //左ボタンをアノテーションビューに追加する。
56
58
  let leftButton = UIButton()
57
59
  leftButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
@@ -78,10 +80,11 @@
78
80
  // 左ボタンが押された場合は青に変える
79
81
  if(control == view.leftCalloutAccessoryView) {
80
82
  markerView.markerTintColor = .blue//機能しない❌
81
- print("markerView:",markerView)
83
+ print("マーカーテスト2markerView:",markerView)//表示なし
82
84
  } else {
83
85
  //右のボタンが押された場合はピンを消す。
84
86
  mapView.removeAnnotation(view.annotation!)//機能する⭕
87
+ print("マーカーテストmarkerView:",markerView)//マーカーテスト3markerView: <MKMarkerAnnotationView: 0x7fb2ec016e00; frame = (121.802 248.686; 28 28); layer = <CALayer: 0x60000042ace0>>
85
88
  }
86
89
  }
87
90
  ```

3

コードを追加

2018/06/01 07:42

投稿

clo.momo
clo.momo

スコア27

title CHANGED
File without changes
body CHANGED
@@ -29,4 +29,59 @@
29
29
  mapView.removeAnnotation(view.annotation!)
30
30
  }
31
31
  }
32
+ ```
33
+
34
+ メソッドの全コードです。
35
+ ```Swift
36
+      markerView = MKMarkerAnnotationView()
37
+
38
+ //選択したアノテーションにボタンを追加する
39
+ func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
40
+ if annotation is MKUserLocation {
41
+ return nil
42
+ }
43
+
44
+ //マーカーアノテーションにコールアウトを表示
45
+ markerView.canShowCallout = true
46
+ let pinID = "pin"
47
+ var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: pinID) as? MKMarkerAnnotationView
48
+ if annotationView == nil {
49
+ annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: pinID)
50
+ //マーカー設置の時アニメーションをつける
51
+ markerView.animatesWhenAdded = true
52
+ //マーカーを緑にする
53
+ markerView.markerTintColor = .green//機能する⭕
54
+
55
+ //左ボタンをアノテーションビューに追加する。
56
+ let leftButton = UIButton()
57
+ leftButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
58
+ leftButton.setTitle("????", for: UIControlState.normal)
59
+ leftButton.setTitleColor(UIColor.black, for: UIControlState.normal)
60
+ markerView.detailCalloutAccessoryView = leftButton
61
+
62
+ //右ボタンをアノテーションビューに追加する。
63
+ let rightButton = UIButton()
64
+ rightButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
65
+ rightButton.setTitle("❌", for: UIControlState.normal)
66
+ rightButton.setTitleColor(UIColor.black, for: UIControlState.normal)
67
+ markerView.rightCalloutAccessoryView = rightButton
68
+
69
+ } else {
70
+ annotationView!.annotation = annotation
71
+ }
72
+
73
+ return markerView
74
+ }
75
+
76
+ //吹き出しアクササリー押下時の呼び出しメソッド
77
+ func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
78
+ // 左ボタンが押された場合は青に変える
79
+ if(control == view.leftCalloutAccessoryView) {
80
+ markerView.markerTintColor = .blue//機能しない❌
81
+ print("markerView:",markerView)
82
+ } else {
83
+ //右のボタンが押された場合はピンを消す。
84
+ mapView.removeAnnotation(view.annotation!)//機能する⭕
85
+ }
86
+ }
32
87
  ```

2

コードミス

2018/06/01 07:34

投稿

clo.momo
clo.momo

スコア27

title CHANGED
File without changes
body CHANGED
@@ -4,10 +4,10 @@
4
4
  0. mapView:viewFor で色が変えられるのだから色を変えるコードは間違っていない
5
5
  0. ところが、mapView:calloutAccessoryControlTapped で同じコードを使うと色が変わらない
6
6
  0. control == view.leftCalloutAccessoryView が間違っている?
7
- 0. 念の為 markerView.markerTintColor = .green をelse{}に入れてみたら色が変わらない
7
+ 0. 念の為 markerView.markerTintColor = .blue をelse{}に入れてみたら色が変わらない
8
- 0. ということは、markerView.markerTintColor = .green は mapView:calloutAccessoryControlTapped では使えない
8
+ 0. ということは、markerView.markerTintColor は mapView:calloutAccessoryControlTapped では使えない
9
9
 
10
- では、markerView.markerTintColor = .green を使わずにアノテーションの色を変えるにはどうすればいいのでしょうか?
10
+ では、markerView.markerTintColor を使わずにアノテーションの色を変えるにはどうすればいいのでしょうか?
11
11
 
12
12
  ```Swift
13
13
  markerView = MKMarkerAnnotationView()

1

コード間違い

2018/06/01 04:55

投稿

clo.momo
clo.momo

スコア27

title CHANGED
File without changes
body CHANGED
@@ -23,7 +23,7 @@
23
23
  func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
24
24
  if(control == view.leftCalloutAccessoryView) {
25
25
            //機能しない ❌
26
- markerView.markerTintColor = .green
26
+ markerView.markerTintColor = .blue
27
27
  } else {
28
28
         //右のボタンが押された場合はピンを消す。   機能する⭕
29
29
  mapView.removeAnnotation(view.annotation!)