回答編集履歴
2
別解を追記
test
CHANGED
@@ -169,3 +169,133 @@
|
|
169
169
|
|
170
170
|
|
171
171
|
![イメージ説明](cdf12a1b054925e8231349ccca00d30f.png)
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
##両方 superView の配下におくのであれば
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
UIImageView にタグをつけて、それで検索することになるのかもしれません。
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
```Swift
|
184
|
+
|
185
|
+
// 検索用のタグ
|
186
|
+
|
187
|
+
var tag = 1
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
//長押しされたら
|
192
|
+
|
193
|
+
@objc func longPress(_ sender: UILongPressGestureRecognizer){
|
194
|
+
|
195
|
+
if sender.state == .began {
|
196
|
+
|
197
|
+
if let parentViewController = parent as? ViewController {
|
198
|
+
|
199
|
+
let parentWidth = parentViewController.view.frame.width
|
200
|
+
|
201
|
+
let height = view.frame.height + 48
|
202
|
+
|
203
|
+
let imageWidth = parentWidth / 4
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
let x = imageWidth * CGFloat(Int.random(in: 0...3))
|
208
|
+
|
209
|
+
let y = height + imageWidth * CGFloat(Int.random(in: 0...3))
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
let baseImageView = UIImageView(frame: CGRect(x: x, y: y, width: parentWidth / 4, height: parentWidth / 4))
|
214
|
+
|
215
|
+
//let upperImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: parentWidth / 4, height: parentWidth / 4))
|
216
|
+
|
217
|
+
let upperImageView = UIImageView(frame: CGRect(x: x, y: y, width: parentWidth / 4, height: parentWidth / 4))
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
baseImageView.image = UIImage()
|
222
|
+
|
223
|
+
baseImageView.backgroundColor = .orange
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
upperImageView.image = UIImage(systemName: "calendar.circle.fill")
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
// 両方の imageView にタグをつける
|
232
|
+
|
233
|
+
baseImageView.tag = tag
|
234
|
+
|
235
|
+
upperImageView.tag = tag + 100
|
236
|
+
|
237
|
+
tag += 1
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(moveView(sender:)))
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
baseImageView.addGestureRecognizer(panGesture)
|
246
|
+
|
247
|
+
baseImageView.isUserInteractionEnabled = true
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
parentViewController.view.addSubview(baseImageView)
|
252
|
+
|
253
|
+
parentViewController.view.addSubview(upperImageView)
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
@objc func moveView(sender: UIPanGestureRecognizer) {
|
264
|
+
|
265
|
+
if let parentViewController = parent as? ViewController, let baseImageView = sender.view as? UIImageView {
|
266
|
+
|
267
|
+
let location = sender.translation(in: parentViewController.view)
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
baseImageView.center.x += location.x
|
272
|
+
|
273
|
+
baseImageView.center.y += location.y
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
// タグを使って upperImageView を探す
|
278
|
+
|
279
|
+
let tag = baseImageView.tag
|
280
|
+
|
281
|
+
print(tag)
|
282
|
+
|
283
|
+
if let upperImageView = parentViewController.view.viewWithTag(tag + 100) as? UIImageView {
|
284
|
+
|
285
|
+
upperImageView.center = baseImageView.center
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
// 移動量をリセット
|
292
|
+
|
293
|
+
sender.setTranslation(.zero, in: parentViewController.view)
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
```
|
1
View Hierarchy の追加
test
CHANGED
@@ -165,3 +165,7 @@
|
|
165
165
|
}
|
166
166
|
|
167
167
|
```
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
![イメージ説明](cdf12a1b054925e8231349ccca00d30f.png)
|