回答編集履歴

1

任意の View を表示させる方法

2020/09/23 15:02

投稿

TsukubaDepot
TsukubaDepot

スコア5086

test CHANGED
@@ -149,3 +149,175 @@
149
149
  }
150
150
 
151
151
  ```
152
+
153
+
154
+
155
+ ##任意の View を表示させる方法
156
+
157
+ ```Swift
158
+
159
+ import UIKit
160
+
161
+ import MapKit
162
+
163
+
164
+
165
+ // UIGestureRecognizerDelegate に準拠させる
166
+
167
+ class ViewController: UIViewController, MKMapViewDelegate, UIGestureRecognizerDelegate {
168
+
169
+ @IBOutlet weak var mapView: MKMapView!
170
+
171
+
172
+
173
+ var annotationCoordinate = MKPointAnnotation()
174
+
175
+
176
+
177
+ override func viewDidLoad() {
178
+
179
+ super.viewDidLoad()
180
+
181
+ // Do any additional setup after loading the view.
182
+
183
+
184
+
185
+ mapView.delegate = self
186
+
187
+
188
+
189
+ // mapView に gestureRecognizer を追加する
190
+
191
+ let gesture = UIPanGestureRecognizer(target: self, action: #selector(receiveGesture(_:)))
192
+
193
+ gesture.delegate = self
194
+
195
+ mapView.addGestureRecognizer(gesture)
196
+
197
+
198
+
199
+ // 緯度・経度を設定
200
+
201
+ let location = CLLocationCoordinate2DMake(35.68154,139.752498)
202
+
203
+ mapView.setCenter(location, animated:true)
204
+
205
+
206
+
207
+ // 初期のアノテーション
208
+
209
+ annotationCoordinate.coordinate = location
210
+
211
+ mapView.addAnnotation(annotationCoordinate)
212
+
213
+
214
+
215
+ // 縮尺を設定
216
+
217
+ var region = mapView.region
218
+
219
+ region.center = location
220
+
221
+ region.span.latitudeDelta = 0.02
222
+
223
+ region.span.longitudeDelta = 0.02
224
+
225
+
226
+
227
+ mapView.setRegion(region,animated:true)
228
+
229
+ mapView.mapType = .standard
230
+
231
+
232
+
233
+ // アニメーションを使うなら
234
+
235
+ // 縦横20ピクセルの視覚を用意する
236
+
237
+ let squareView = UIView(frame: .init(x: 0, y: 0, width: 20, height: 20))
238
+
239
+ squareView.layer.borderWidth = 2
240
+
241
+ squareView.layer.borderColor = UIColor.red.cgColor
242
+
243
+
244
+
245
+ // 点滅処理
246
+
247
+ UIView.animateKeyframes(withDuration: 0.5, delay: 0.0, options: [.autoreverse, .repeat]) {
248
+
249
+ squareView.alpha = 0.0
250
+
251
+ } completion: { _ in
252
+
253
+ }
254
+
255
+
256
+
257
+ // mapView の region.center として設定されている CGPoint を取得して、squareView の中心にする
258
+
259
+ squareView.center = mapView.convert(mapView.region.center, toPointTo: mapView)
260
+
261
+ mapView.addSubview(squareView)
262
+
263
+ }
264
+
265
+
266
+
267
+ // delegate: 複数の GestureRecognizer を有効にする
268
+
269
+ func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
270
+
271
+ return true
272
+
273
+ }
274
+
275
+
276
+
277
+ @objc func receiveGesture(_ gestureRecognizer : UIPanGestureRecognizer) {
278
+
279
+ print(#function)
280
+
281
+ if let mapView = gestureRecognizer.view as? MKMapView {
282
+
283
+ moveAnnotation(mapView)
284
+
285
+ }
286
+
287
+ }
288
+
289
+
290
+
291
+ func moveAnnotation(_ mapView: MKMapView) {
292
+
293
+ // 地図の中央座標
294
+
295
+ annotationCoordinate.coordinate = mapView.centerCoordinate
296
+
297
+ }
298
+
299
+
300
+
301
+ // delegate: ドラッグ終了
302
+
303
+ func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
304
+
305
+ print(#function)
306
+
307
+ moveAnnotation(mapView)
308
+
309
+ }
310
+
311
+
312
+
313
+ // delegate: ドラッグ開始
314
+
315
+ func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
316
+
317
+ print(#function)
318
+
319
+ }
320
+
321
+ }
322
+
323
+ ```