質問編集履歴
2
移動に関するコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -74,6 +74,10 @@
|
|
74
74
|
|
75
75
|
var screenHeight:CGFloat = 0
|
76
76
|
|
77
|
+
var photowidth:CGFloat = 0
|
78
|
+
|
79
|
+
var photoheight:CGFloat = 0
|
80
|
+
|
77
81
|
var pickerphotoheight:CGFloat = 0
|
78
82
|
|
79
83
|
var imageHeight:CGFloat = 0
|
@@ -264,6 +268,124 @@
|
|
264
268
|
|
265
269
|
|
266
270
|
|
271
|
+
//画像の移動↓
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
//タッチしたビューの中心とタッチした場所の座標のズレを保持する変数
|
276
|
+
|
277
|
+
var gapX:CGFloat = 0.0 // x座標
|
278
|
+
|
279
|
+
var gapY:CGFloat = 0.0 // y座標
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
// タッチした位置で最初に見つかったところにあるビューを取得してしまおうという魂胆
|
286
|
+
|
287
|
+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
288
|
+
|
289
|
+
// 最初にタッチした指のみ取得
|
290
|
+
|
291
|
+
if let touch = touches.first {
|
292
|
+
|
293
|
+
print("touchbegin!")
|
294
|
+
|
295
|
+
// タッチしたビューをviewプロパティで取得する
|
296
|
+
|
297
|
+
if let touchedView = touch.view {
|
298
|
+
|
299
|
+
// tag1のものを動かす
|
300
|
+
|
301
|
+
if touchedView.tag == 1 {
|
302
|
+
|
303
|
+
// タッチした場所とタッチしたビューの中心座標がどうずれているか?
|
304
|
+
|
305
|
+
gapX = touch.location(in: view).x - touchedView.center.x
|
306
|
+
|
307
|
+
gapY = touch.location(in: view).y - touchedView.center.y
|
308
|
+
|
309
|
+
// 例えば、タッチしたビューの中心のxが50、タッチした場所のxが60→中心から10ずれ
|
310
|
+
|
311
|
+
// この場合、指を100に持って行ったらビューの中心は90にしたい
|
312
|
+
|
313
|
+
// ビューの中心90 = 持って行った場所100 - ずれ10
|
314
|
+
|
315
|
+
touchedView.center = CGPoint(x: touch.location(in: view).x - gapX, y: touch.location(in: view).y - gapY)
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
}
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
|
328
|
+
|
329
|
+
// touchesBeganと同じ処理だが、gapXとgapYはタッチ中で同じものを使い続ける
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
// 最初にタッチした指のみ取得
|
334
|
+
|
335
|
+
if let touch = touches.first {
|
336
|
+
|
337
|
+
// タッチしたビューをviewプロパティで取得する
|
338
|
+
|
339
|
+
if let touchedView = touch.view {
|
340
|
+
|
341
|
+
// tag1のものを動かす
|
342
|
+
|
343
|
+
if touchedView.tag == 1 {
|
344
|
+
|
345
|
+
// gapX,gapYの取得は行わない
|
346
|
+
|
347
|
+
touchedView.center = CGPoint(x: touch.location(in: view).x - gapX, y: touch.location(in: view).y - gapY)
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
}
|
354
|
+
|
355
|
+
}
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
// gapXとgapYの初期化
|
368
|
+
|
369
|
+
gapX = 0.0
|
370
|
+
|
371
|
+
gapY = 0.0
|
372
|
+
|
373
|
+
}
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
|
378
|
+
|
379
|
+
// touchesEndedと同じ処理
|
380
|
+
|
381
|
+
self.touchesEnded(touches, with: event)
|
382
|
+
|
383
|
+
}
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
|
388
|
+
|
267
389
|
//カメラロールから写真を選択する処理
|
268
390
|
|
269
391
|
@IBAction func choosePicture() {
|
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -62,17 +62,27 @@
|
|
62
62
|
|
63
63
|
|
64
64
|
|
65
|
-
|
65
|
+
var scale:CGFloat = 1.0
|
66
|
+
|
66
|
-
|
67
|
+
var Rotation:CGFloat = 0.0
|
68
|
+
|
67
|
-
|
69
|
+
var affine1:CGAffineTransform = CGAffineTransform()
|
70
|
+
|
71
|
+
var affine2:CGAffineTransform = CGAffineTransform()
|
68
72
|
|
69
73
|
var screenWidth:CGFloat = 0
|
70
74
|
|
71
75
|
var screenHeight:CGFloat = 0
|
72
76
|
|
77
|
+
var pickerphotoheight:CGFloat = 0
|
78
|
+
|
73
79
|
var imageHeight:CGFloat = 0
|
74
80
|
|
75
|
-
var
|
81
|
+
var imagerect:CGRect = CGRect(x:0,y:0,width:0,height:0)
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
76
86
|
|
77
87
|
|
78
88
|
|
@@ -126,12 +136,22 @@
|
|
126
136
|
|
127
137
|
|
128
138
|
|
129
|
-
//デリゲート先に自分を設定する。
|
139
|
+
//デリゲート先に自分を設定する。
|
130
140
|
|
131
141
|
rotationRecognizer.delegate = self
|
132
142
|
|
133
143
|
pinchRecognizer.delegate = self
|
134
144
|
|
145
|
+
//アフィン変換の初期値を設定する。
|
146
|
+
|
147
|
+
prevEndPinch = getphoto.transform
|
148
|
+
|
149
|
+
prevEndRotate = getphoto.transform
|
150
|
+
|
151
|
+
prevPinch = getphoto.transform
|
152
|
+
|
153
|
+
prevRotate = getphoto.transform
|
154
|
+
|
135
155
|
|
136
156
|
|
137
157
|
|
@@ -148,12 +168,102 @@
|
|
148
168
|
|
149
169
|
|
150
170
|
|
151
|
-
|
171
|
+
//ドラッグ終了時のアフィン変換
|
172
|
+
|
173
|
+
var prevEndPinch:CGAffineTransform = CGAffineTransform()
|
174
|
+
|
175
|
+
var prevEndRotate:CGAffineTransform = CGAffineTransform()
|
176
|
+
|
177
|
+
//ドラッグ中の前回アフィン変換
|
178
|
+
|
179
|
+
var prevPinch:CGAffineTransform = CGAffineTransform()
|
180
|
+
|
181
|
+
var prevRotate:CGAffineTransform = CGAffineTransform()
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
@IBAction func pinchPhoto(_ sender: UIPinchGestureRecognizer) {
|
190
|
+
|
191
|
+
print("pinch!")
|
192
|
+
|
193
|
+
//前回ドラッグ終了時の拡大縮小を引き継いだアフィン変換を行う。
|
194
|
+
|
195
|
+
let nowPinch =
|
196
|
+
|
197
|
+
prevEndPinch.scaledBy(x: sender.scale, y: sender.scale)
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
//拡大縮小と回転のアフィン変換を合わせたものを登録する。
|
202
|
+
|
203
|
+
getphoto.transform = prevRotate.concatenating(nowPinch)
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
//今回の拡大縮小のアフィン変換をクラス変数に保存する。
|
210
|
+
|
211
|
+
prevPinch = nowPinch
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
if(sender.state == UIGestureRecognizerState.ended) {
|
216
|
+
|
217
|
+
//ドラッグ終了時の拡大終了のアフィン変換をクラス変数に保存する。
|
218
|
+
|
219
|
+
prevEndPinch = nowPinch
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
}
|
152
224
|
|
153
225
|
|
154
226
|
|
155
227
|
|
156
228
|
|
229
|
+
@IBAction func rotatePhoto(_ sender: UIRotationGestureRecognizer){
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
print("rotate!")
|
234
|
+
|
235
|
+
//前回ドラッグ終了時の回転を引き継いだアフィン変換を行う。
|
236
|
+
|
237
|
+
let nowRotate = prevEndRotate.rotated(by: sender.rotation)
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
//拡大縮小と回転のアフィン変換を合わせたものをラベルに登録する。
|
242
|
+
|
243
|
+
getphoto.transform = prevPinch.concatenating(nowRotate)
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
//今回の回転のアフィン変換をクラス変数に保存する。
|
248
|
+
|
249
|
+
prevRotate = nowRotate
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
if(sender.state == UIGestureRecognizerState.ended) {
|
254
|
+
|
255
|
+
//ドラッグ終了時の回転のアフィン変換をクラス変数に保存する。
|
256
|
+
|
257
|
+
prevEndRotate = nowRotate
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
157
267
|
//カメラロールから写真を選択する処理
|
158
268
|
|
159
269
|
@IBAction func choosePicture() {
|
@@ -352,203 +462,7 @@
|
|
352
462
|
|
353
463
|
|
354
464
|
|
355
|
-
|
465
|
+
|
356
|
-
|
357
|
-
func aspectFit(contentSize: CGSize, stretchble: Bool, integer: Bool) -> CGRect {
|
358
|
-
|
359
|
-
let xZoom = width / contentSize.width
|
360
|
-
|
361
|
-
let yZoom = height / contentSize.height
|
362
|
-
|
363
|
-
let zoom = stretchble ? min(xZoom, yZoom) : min(xZoom, yZoom, 1)
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
if integer {
|
368
|
-
|
369
|
-
let newWidth = max(Int(contentSize.width * zoom), 1)
|
370
|
-
|
371
|
-
let newHeight = max(Int(contentSize.height * zoom), 1)
|
372
|
-
|
373
|
-
let newX = Int(origin.x) + (Int(width) - newWidth) / 2
|
374
|
-
|
375
|
-
let newY = Int(origin.y) + (Int(height) - newHeight) / 2
|
376
|
-
|
377
|
-
return CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
|
378
|
-
|
379
|
-
} else {
|
380
|
-
|
381
|
-
let newWidth = contentSize.width * zoom
|
382
|
-
|
383
|
-
let newHeight = contentSize.height * zoom
|
384
|
-
|
385
|
-
let newX = origin.x + (width - newWidth) / 2
|
386
|
-
|
387
|
-
let newY = origin.y + (height - newHeight) / 2
|
388
|
-
|
389
|
-
return CGRect(x: newX, y: newY, width: newWidth, height: newHeight)
|
390
|
-
|
391
|
-
}
|
392
|
-
|
393
|
-
}
|
394
|
-
|
395
|
-
}
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
extension CGRect
|
400
|
-
|
401
|
-
{
|
402
|
-
|
403
|
-
/** Creates a rectangle with the given center and dimensions
|
404
|
-
|
405
|
-
- parameter center: The center of the new rectangle
|
406
|
-
|
407
|
-
- parameter size: The dimensions of the new rectangle
|
408
|
-
|
409
|
-
*/
|
410
|
-
|
411
|
-
init(center: CGPoint, size: CGSize)
|
412
|
-
|
413
|
-
{
|
414
|
-
|
415
|
-
self.init(x: center.x - size.width / 2, y: center.y - size.height / 2, width: size.width, height: size.height)
|
416
|
-
|
417
|
-
}
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
/** the coordinates of this rectangles center */
|
422
|
-
|
423
|
-
var center: CGPoint
|
424
|
-
|
425
|
-
{
|
426
|
-
|
427
|
-
get { return CGPoint(x: centerX, y: centerY) }
|
428
|
-
|
429
|
-
set { centerX = newValue.x; centerY = newValue.y }
|
430
|
-
|
431
|
-
}
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
/** the x-coordinate of this rectangles center
|
436
|
-
|
437
|
-
- note: Acts as a settable midX
|
438
|
-
|
439
|
-
- returns: The x-coordinate of the center
|
440
|
-
|
441
|
-
*/
|
442
|
-
|
443
|
-
var centerX: CGFloat
|
444
|
-
|
445
|
-
{
|
446
|
-
|
447
|
-
get { return midX }
|
448
|
-
|
449
|
-
set { origin.x = newValue - width * 0.5 }
|
450
|
-
|
451
|
-
}
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
/** the y-coordinate of this rectangles center
|
456
|
-
|
457
|
-
- note: Acts as a settable midY
|
458
|
-
|
459
|
-
- returns: The y-coordinate of the center
|
460
|
-
|
461
|
-
*/
|
462
|
-
|
463
|
-
var centerY: CGFloat
|
464
|
-
|
465
|
-
{
|
466
|
-
|
467
|
-
get { return midY }
|
468
|
-
|
469
|
-
set { origin.y = newValue - height * 0.5 }
|
470
|
-
|
471
|
-
}
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
// MARK: - "with" convenience functions
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
/** Same-sized rectangle with a new center
|
480
|
-
|
481
|
-
- parameter center: The new center, ignored if nil
|
482
|
-
|
483
|
-
- returns: A new rectangle with the same size and a new center
|
484
|
-
|
485
|
-
*/
|
486
|
-
|
487
|
-
func with(center: CGPoint?) -> CGRect
|
488
|
-
|
489
|
-
{
|
490
|
-
|
491
|
-
return CGRect(center: center ?? self.center, size: size)
|
492
|
-
|
493
|
-
}
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
/** Same-sized rectangle with a new center-x
|
498
|
-
|
499
|
-
- parameter centerX: The new center-x, ignored if nil
|
500
|
-
|
501
|
-
- returns: A new rectangle with the same size and a new center
|
502
|
-
|
503
|
-
*/
|
504
|
-
|
505
|
-
func with(centerX: CGFloat?) -> CGRect
|
506
|
-
|
507
|
-
{
|
508
|
-
|
509
|
-
return CGRect(center: CGPoint(x: centerX ?? self.centerX, y: centerY), size: size)
|
510
|
-
|
511
|
-
}
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
/** Same-sized rectangle with a new center-y
|
516
|
-
|
517
|
-
- parameter centerY: The new center-y, ignored if nil
|
518
|
-
|
519
|
-
- returns: A new rectangle with the same size and a new center
|
520
|
-
|
521
|
-
*/
|
522
|
-
|
523
|
-
func with(centerY: CGFloat?) -> CGRect
|
524
|
-
|
525
|
-
{
|
526
|
-
|
527
|
-
return CGRect(center: CGPoint(x: centerX, y: centerY ?? self.centerY), size: size)
|
528
|
-
|
529
|
-
}
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
/** Same-sized rectangle with a new center-x and center-y
|
534
|
-
|
535
|
-
- parameter centerX: The new center-x, ignored if nil
|
536
|
-
|
537
|
-
- parameter centerY: The new center-y, ignored if nil
|
538
|
-
|
539
|
-
- returns: A new rectangle with the same size and a new center
|
540
|
-
|
541
|
-
*/
|
542
|
-
|
543
|
-
func with(centerX: CGFloat?, centerY: CGFloat?) -> CGRect
|
544
|
-
|
545
|
-
{
|
546
|
-
|
547
|
-
return CGRect(center: CGPoint(x: centerX ?? self.centerX, y: centerY ?? self.centerY), size: size)
|
548
|
-
|
549
|
-
}
|
550
|
-
|
551
|
-
}
|
552
466
|
|
553
467
|
|
554
468
|
|