回答編集履歴
1
任意の View を表示させる方法
answer
CHANGED
@@ -73,4 +73,90 @@
|
|
73
73
|
print(#function)
|
74
74
|
}
|
75
75
|
}
|
76
|
+
```
|
77
|
+
|
78
|
+
##任意の View を表示させる方法
|
79
|
+
```Swift
|
80
|
+
import UIKit
|
81
|
+
import MapKit
|
82
|
+
|
83
|
+
// UIGestureRecognizerDelegate に準拠させる
|
84
|
+
class ViewController: UIViewController, MKMapViewDelegate, UIGestureRecognizerDelegate {
|
85
|
+
@IBOutlet weak var mapView: MKMapView!
|
86
|
+
|
87
|
+
var annotationCoordinate = MKPointAnnotation()
|
88
|
+
|
89
|
+
override func viewDidLoad() {
|
90
|
+
super.viewDidLoad()
|
91
|
+
// Do any additional setup after loading the view.
|
92
|
+
|
93
|
+
mapView.delegate = self
|
94
|
+
|
95
|
+
// mapView に gestureRecognizer を追加する
|
96
|
+
let gesture = UIPanGestureRecognizer(target: self, action: #selector(receiveGesture(_:)))
|
97
|
+
gesture.delegate = self
|
98
|
+
mapView.addGestureRecognizer(gesture)
|
99
|
+
|
100
|
+
// 緯度・経度を設定
|
101
|
+
let location = CLLocationCoordinate2DMake(35.68154,139.752498)
|
102
|
+
mapView.setCenter(location, animated:true)
|
103
|
+
|
104
|
+
// 初期のアノテーション
|
105
|
+
annotationCoordinate.coordinate = location
|
106
|
+
mapView.addAnnotation(annotationCoordinate)
|
107
|
+
|
108
|
+
// 縮尺を設定
|
109
|
+
var region = mapView.region
|
110
|
+
region.center = location
|
111
|
+
region.span.latitudeDelta = 0.02
|
112
|
+
region.span.longitudeDelta = 0.02
|
113
|
+
|
114
|
+
mapView.setRegion(region,animated:true)
|
115
|
+
mapView.mapType = .standard
|
116
|
+
|
117
|
+
// アニメーションを使うなら
|
118
|
+
// 縦横20ピクセルの視覚を用意する
|
119
|
+
let squareView = UIView(frame: .init(x: 0, y: 0, width: 20, height: 20))
|
120
|
+
squareView.layer.borderWidth = 2
|
121
|
+
squareView.layer.borderColor = UIColor.red.cgColor
|
122
|
+
|
123
|
+
// 点滅処理
|
124
|
+
UIView.animateKeyframes(withDuration: 0.5, delay: 0.0, options: [.autoreverse, .repeat]) {
|
125
|
+
squareView.alpha = 0.0
|
126
|
+
} completion: { _ in
|
127
|
+
}
|
128
|
+
|
129
|
+
// mapView の region.center として設定されている CGPoint を取得して、squareView の中心にする
|
130
|
+
squareView.center = mapView.convert(mapView.region.center, toPointTo: mapView)
|
131
|
+
mapView.addSubview(squareView)
|
132
|
+
}
|
133
|
+
|
134
|
+
// delegate: 複数の GestureRecognizer を有効にする
|
135
|
+
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
|
136
|
+
return true
|
137
|
+
}
|
138
|
+
|
139
|
+
@objc func receiveGesture(_ gestureRecognizer : UIPanGestureRecognizer) {
|
140
|
+
print(#function)
|
141
|
+
if let mapView = gestureRecognizer.view as? MKMapView {
|
142
|
+
moveAnnotation(mapView)
|
143
|
+
}
|
144
|
+
}
|
145
|
+
|
146
|
+
func moveAnnotation(_ mapView: MKMapView) {
|
147
|
+
// 地図の中央座標
|
148
|
+
annotationCoordinate.coordinate = mapView.centerCoordinate
|
149
|
+
}
|
150
|
+
|
151
|
+
// delegate: ドラッグ終了
|
152
|
+
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
|
153
|
+
print(#function)
|
154
|
+
moveAnnotation(mapView)
|
155
|
+
}
|
156
|
+
|
157
|
+
// delegate: ドラッグ開始
|
158
|
+
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
|
159
|
+
print(#function)
|
160
|
+
}
|
161
|
+
}
|
76
162
|
```
|