質問編集履歴
1
内容の追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -86,15 +86,80 @@
|
|
86
86
|
imageView.frame.origin.y += deltaY
|
87
87
|
|
88
88
|
}
|
89
|
+
|
90
|
+
@objc func pinchAction(gesture: UIPinchGestureRecognizer) {
|
91
|
+
|
92
|
+
if gesture.state == UIGestureRecognizerState.began {
|
93
|
+
// ピンチを開始したときの画像の中心点を保存しておく
|
94
|
+
pinchStartImageCenter = imageView.center
|
95
|
+
touchPoint1 = gesture.location(ofTouch: 0, in: self.view)
|
96
|
+
touchPoint2 = gesture.location(ofTouch: 1, in: self.view)
|
97
|
+
|
98
|
+
// 指の中間点を求めて保存しておく
|
99
|
+
// UIGestureRecognizerState.Changedで毎回求めた場合、ピンチ状態で片方の指だけ動かしたときに中心点がずれておかしな位置でズームされるため
|
100
|
+
pichCenter = CGPoint(x: (touchPoint1.x + touchPoint2.x) / 2, y: (touchPoint1.y + touchPoint2.y) / 2)
|
101
|
+
|
102
|
+
} else if gesture.state == UIGestureRecognizerState.changed {
|
103
|
+
// ピンチジェスチャー・変更中
|
104
|
+
var pinchScale : CGFloat// ピンチを開始してからの拡大率。差分ではない
|
105
|
+
if gesture.scale > 1 {
|
106
|
+
pinchScale = 1 + gesture.scale/100
|
107
|
+
}else{
|
108
|
+
pinchScale = gesture.scale
|
109
|
+
}
|
110
|
+
if pinchScale*self.imageView.frame.width < editPhotoView.frame.width {
|
111
|
+
pinchScale = editPhotoView.frame.width/self.imageView.frame.width
|
112
|
+
}
|
113
|
+
scaleZoomedInOut *= pinchScale
|
114
|
+
|
115
|
+
// ピンチした位置を中心としてズーム(イン/アウト)するように、画像の中心位置をずらす
|
116
|
+
let newCenter = CGPoint(x: pinchStartImageCenter.x - ((pichCenter.x - pinchStartImageCenter.x) * pinchScale - (pichCenter.x - pinchStartImageCenter.x)),y: pinchStartImageCenter.y - ((pichCenter.y - pinchStartImageCenter.y) * pinchScale - (pichCenter.y - pinchStartImageCenter.y)))
|
117
|
+
self.imageView.frame.size = CGSize(width: pinchScale*self.imageView.frame.width, height: pinchScale*self.imageView.frame.height)
|
118
|
+
imageView.center = newCenter
|
119
|
+
}
|
120
|
+
}
|
121
|
+
|
122
|
+
//ダブルタップは、ダブルタップを行うとタップをしたところを中心にUIImageViewが2倍の大きさになる
|
123
|
+
@objc func doubleTapAction(gesture: UITapGestureRecognizer) {
|
124
|
+
|
125
|
+
if gesture.state == UIGestureRecognizerState.ended {
|
126
|
+
|
127
|
+
let doubleTapStartCenter = imageView.center
|
128
|
+
let doubleTapScale: CGFloat = 2.0 // ダブルタップでは現在のスケールの2倍にする
|
129
|
+
scaleZoomedInOut *= doubleTapScale
|
130
|
+
let tapPoint = gesture.location(in: self.view)
|
131
|
+
let newCenter = CGPoint(x:
|
132
|
+
doubleTapStartCenter.x - ((tapPoint.x - doubleTapStartCenter.x) * doubleTapScale - (tapPoint.x - doubleTapStartCenter.x)),
|
133
|
+
y: doubleTapStartCenter.y - ((tapPoint.y - doubleTapStartCenter.y) * doubleTapScale - (tapPoint.y - doubleTapStartCenter.y)))
|
134
|
+
|
135
|
+
// 拡大・縮小とタップ位置に合わせた中心点の移動
|
136
|
+
UIView.animate(withDuration: 0.3, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: {() -> Void in
|
137
|
+
self.imageView.frame.size = CGSize(width: doubleTapScale*self.imageView.frame.width, height: doubleTapScale*self.imageView.frame.height)
|
138
|
+
self.imageView.center = newCenter
|
139
|
+
}, completion: nil)
|
140
|
+
}
|
141
|
+
}
|
89
142
|
```
|
90
143
|
ドラッグして画像を動かした際に移動した距離を割り出し、
|
91
144
|
トリミング対象画像を表示するためのUIImageViewのorigin.x(y)をインクリメントすることで
|
92
145
|
動作しております。
|
93
146
|
|
94
147
|
### 試したこと
|
148
|
+
|
149
|
+
```swift
|
150
|
+
if imageView.frame.origin.y < 0{
|
151
|
+
imageView.frame.origin.y = 0
|
152
|
+
}else if imageView.frame.origin.y > 0{
|
153
|
+
imageView.frame.origin.y = 0
|
154
|
+
}else if imageView.frame.origin.x > 0{
|
155
|
+
imageView.frame.origin.x = 0
|
156
|
+
}
|
157
|
+
```
|
158
|
+
上記のやり方ではトリミング枠から出ないものの、ピンチインアウト時や、ズームインアウト時におかしな挙動になり想定の動きかたをしてくれませんでした。
|
159
|
+
|
95
160
|
画像の幅・高さを取得して、=> let imageWidth = sourceImage.size.width
|
96
161
|
|
97
|
-
「トリミング対象画像を表示するためのUIImageViewが、let imageWidth 以上になったらドラッグ移動させない」ようなやり方で試
|
162
|
+
「トリミング対象画像を表示するためのUIImageViewが、let imageWidth 以上になったらドラッグ移動させない」ようなやり方で試そうと思っています。
|
98
163
|
|
99
|
-
|
164
|
+
他に拡張性の高いやり方など、ご存知の方がいらっしゃればご教授頂けないでしょうか!!!!!
|
100
165
|
よろしくお願いします。
|