質問編集履歴

1

内容の追記

2018/09/23 10:59

投稿

RyomaD
RyomaD

スコア34

test CHANGED
File without changes
test CHANGED
@@ -174,6 +174,112 @@
174
174
 
175
175
  }
176
176
 
177
+
178
+
179
+ @objc func pinchAction(gesture: UIPinchGestureRecognizer) {
180
+
181
+
182
+
183
+ if gesture.state == UIGestureRecognizerState.began {
184
+
185
+ // ピンチを開始したときの画像の中心点を保存しておく
186
+
187
+ pinchStartImageCenter = imageView.center
188
+
189
+ touchPoint1 = gesture.location(ofTouch: 0, in: self.view)
190
+
191
+ touchPoint2 = gesture.location(ofTouch: 1, in: self.view)
192
+
193
+
194
+
195
+ // 指の中間点を求めて保存しておく
196
+
197
+ // UIGestureRecognizerState.Changedで毎回求めた場合、ピンチ状態で片方の指だけ動かしたときに中心点がずれておかしな位置でズームされるため
198
+
199
+ pichCenter = CGPoint(x: (touchPoint1.x + touchPoint2.x) / 2, y: (touchPoint1.y + touchPoint2.y) / 2)
200
+
201
+
202
+
203
+ } else if gesture.state == UIGestureRecognizerState.changed {
204
+
205
+ // ピンチジェスチャー・変更中
206
+
207
+ var pinchScale : CGFloat// ピンチを開始してからの拡大率。差分ではない
208
+
209
+ if gesture.scale > 1 {
210
+
211
+ pinchScale = 1 + gesture.scale/100
212
+
213
+ }else{
214
+
215
+ pinchScale = gesture.scale
216
+
217
+ }
218
+
219
+ if pinchScale*self.imageView.frame.width < editPhotoView.frame.width {
220
+
221
+ pinchScale = editPhotoView.frame.width/self.imageView.frame.width
222
+
223
+ }
224
+
225
+ scaleZoomedInOut *= pinchScale
226
+
227
+
228
+
229
+ // ピンチした位置を中心としてズーム(イン/アウト)するように、画像の中心位置をずらす
230
+
231
+ 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)))
232
+
233
+ self.imageView.frame.size = CGSize(width: pinchScale*self.imageView.frame.width, height: pinchScale*self.imageView.frame.height)
234
+
235
+ imageView.center = newCenter
236
+
237
+ }
238
+
239
+ }
240
+
241
+
242
+
243
+ //ダブルタップは、ダブルタップを行うとタップをしたところを中心にUIImageViewが2倍の大きさになる
244
+
245
+ @objc func doubleTapAction(gesture: UITapGestureRecognizer) {
246
+
247
+
248
+
249
+ if gesture.state == UIGestureRecognizerState.ended {
250
+
251
+
252
+
253
+ let doubleTapStartCenter = imageView.center
254
+
255
+ let doubleTapScale: CGFloat = 2.0 // ダブルタップでは現在のスケールの2倍にする
256
+
257
+ scaleZoomedInOut *= doubleTapScale
258
+
259
+ let tapPoint = gesture.location(in: self.view)
260
+
261
+ let newCenter = CGPoint(x:
262
+
263
+ doubleTapStartCenter.x - ((tapPoint.x - doubleTapStartCenter.x) * doubleTapScale - (tapPoint.x - doubleTapStartCenter.x)),
264
+
265
+ y: doubleTapStartCenter.y - ((tapPoint.y - doubleTapStartCenter.y) * doubleTapScale - (tapPoint.y - doubleTapStartCenter.y)))
266
+
267
+
268
+
269
+ // 拡大・縮小とタップ位置に合わせた中心点の移動
270
+
271
+ UIView.animate(withDuration: 0.3, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: {() -> Void in
272
+
273
+ self.imageView.frame.size = CGSize(width: doubleTapScale*self.imageView.frame.width, height: doubleTapScale*self.imageView.frame.height)
274
+
275
+ self.imageView.center = newCenter
276
+
277
+ }, completion: nil)
278
+
279
+ }
280
+
281
+ }
282
+
177
283
  ```
178
284
 
179
285
  ドラッグして画像を動かした際に移動した距離を割り出し、
@@ -186,14 +292,38 @@
186
292
 
187
293
  ### 試したこと
188
294
 
295
+
296
+
297
+ ```swift
298
+
299
+ if imageView.frame.origin.y < 0{
300
+
301
+ imageView.frame.origin.y = 0
302
+
303
+ }else if imageView.frame.origin.y > 0{
304
+
305
+ imageView.frame.origin.y = 0
306
+
307
+ }else if imageView.frame.origin.x > 0{
308
+
309
+ imageView.frame.origin.x = 0
310
+
311
+ }
312
+
313
+ ```
314
+
315
+ 上記のやり方ではトリミング枠から出ないものの、ピンチインアウト時や、ズームインアウト時におかしな挙動になり想定の動きかたをしてくれませんでした。
316
+
317
+
318
+
189
319
  画像の幅・高さを取得して、=> let imageWidth = sourceImage.size.width
190
320
 
191
321
 
192
322
 
193
- 「トリミング対象画像を表示するためのUIImageViewが、let imageWidth 以上になったらドラッグ移動させない」ようなやり方で試してみたのですが、上手く機能せず困っております。
323
+ 「トリミング対象画像を表示するためのUIImageViewが、let imageWidth 以上になったらドラッグ移動させない」ようなやり方で試そうと思ってます。
194
-
195
-
196
-
324
+
325
+
326
+
197
- どなたかやり方ご存知の方がいらっしゃればご教授頂けないでしょうか!!!!!
327
+ 他に拡張性の高いやり方など、ご存知の方がいらっしゃればご教授頂けないでしょうか!!!!!
198
328
 
199
329
  よろしくお願いします。