質問編集履歴
1
修正後のソースを載せました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -67,3 +67,259 @@
|
|
67
67
|
このエラーを消すにはどうすればよいか、教えていただけると助かります。
|
68
68
|
|
69
69
|
よろしくお願いします。
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
###修正後のソース全文
|
74
|
+
|
75
|
+
```swift
|
76
|
+
|
77
|
+
import UIKit
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
class ViewController2: UIViewController, UIGestureRecognizerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
// 画像の拡大率
|
88
|
+
|
89
|
+
var currentScale:CGFloat = 1.0
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
let firstImg: UIImage = UIImage(named: "IMG_6632.JPG")!
|
94
|
+
|
95
|
+
@IBOutlet weak var getphoto: UIImageView!
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
override func viewDidLoad() {
|
100
|
+
|
101
|
+
super.viewDidLoad()
|
102
|
+
|
103
|
+
getphoto.image = firstImg
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
// タッチ操作を enable
|
108
|
+
|
109
|
+
getphoto.isUserInteractionEnabled = true
|
110
|
+
|
111
|
+
self.view?.addSubview(getphoto)
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
// imageViewにジェスチャーレコグナイザを設定する(ピンチ) 拡大縮小
|
116
|
+
|
117
|
+
//下記の文1でエラー発生
|
118
|
+
|
119
|
+
//Use of local variable 'pinchAction(sender:)' before its declaration
|
120
|
+
|
121
|
+
/* 1 */ let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(sender:)))
|
122
|
+
|
123
|
+
getphoto.addGestureRecognizer(pinchGesture)
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
///////////////////////////////////////////画像の移動↓
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
// タッチしたビューの中心とタッチした場所の座標のズレを保持する変数
|
132
|
+
|
133
|
+
var gapX:CGFloat = 0.0 // x座標
|
134
|
+
|
135
|
+
var gapY:CGFloat = 0.0 // y座標
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
|
144
|
+
|
145
|
+
// 最初にタッチした指のみ取得
|
146
|
+
|
147
|
+
if let touch = touches.first {
|
148
|
+
|
149
|
+
// タッチしたビューをviewプロパティで取得する
|
150
|
+
|
151
|
+
if let touchedView = touch.view {
|
152
|
+
|
153
|
+
// tag1のものを動かす
|
154
|
+
|
155
|
+
if touchedView.tag == 1 {
|
156
|
+
|
157
|
+
// タッチした場所とタッチしたビューの中心座標がどうずれているか?
|
158
|
+
|
159
|
+
gapX = touch.location(in: view).x - touchedView.center.x
|
160
|
+
|
161
|
+
gapY = touch.location(in: view).y - touchedView.center.y
|
162
|
+
|
163
|
+
// 例えば、タッチしたビューの中心のxが50、タッチした場所のxが60→中心から10ずれ
|
164
|
+
|
165
|
+
// この場合、指を100に持って行ったらビューの中心は90にしたい
|
166
|
+
|
167
|
+
// ビューの中心90 = 持って行った場所100 - ずれ10
|
168
|
+
|
169
|
+
touchedView.center = CGPoint(x: touch.location(in: view).x - gapX, y: touch.location(in: view).y - gapY)
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
|
182
|
+
|
183
|
+
// touchesBeganと同じ処理だが、gapXとgapYはタッチ中で同じものを使い続ける
|
184
|
+
|
185
|
+
// 最初にタッチした指のみ取得
|
186
|
+
|
187
|
+
if let touch = touches.first {
|
188
|
+
|
189
|
+
// タッチしたビューをviewプロパティで取得する
|
190
|
+
|
191
|
+
if let touchedView = touch.view {
|
192
|
+
|
193
|
+
// tag1のものを動かす
|
194
|
+
|
195
|
+
if touchedView.tag == 1 {
|
196
|
+
|
197
|
+
// gapX,gapYの取得は行わない
|
198
|
+
|
199
|
+
touchedView.center = CGPoint(x: touch.location(in: view).x - gapX, y: touch.location(in: view).y - gapY)
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
|
212
|
+
|
213
|
+
// gapXとgapYの初期化
|
214
|
+
|
215
|
+
gapX = 0.0
|
216
|
+
|
217
|
+
gapY = 0.0
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
|
224
|
+
|
225
|
+
// touchesEndedと同じ処理
|
226
|
+
|
227
|
+
self.touchesEnded(touches, with: event)
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
///////////////////////////////////////////拡大縮小↓
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
func pinchAction(sender: UIPinchGestureRecognizer) {
|
238
|
+
|
239
|
+
// imageViewを拡大縮小する
|
240
|
+
|
241
|
+
// ピンチ中の拡大率は0.3〜2.5倍、指を離した時の拡大率は0.5〜2.0倍とする
|
242
|
+
|
243
|
+
switch sender.state {
|
244
|
+
|
245
|
+
case .began, .changed:
|
246
|
+
|
247
|
+
// senderのscaleは、指を動かしていない状態が1.0となる
|
248
|
+
|
249
|
+
// 現在の拡大率に、(scaleから1を引いたもの) / 10(補正率)を加算する
|
250
|
+
|
251
|
+
currentScale = currentScale + (sender.scale - 1) / 10
|
252
|
+
|
253
|
+
// 拡大率が基準から外れる場合は、補正する
|
254
|
+
|
255
|
+
if currentScale < 0.3 {
|
256
|
+
|
257
|
+
currentScale = 0.3
|
258
|
+
|
259
|
+
} else if currentScale > 2.5 {
|
260
|
+
|
261
|
+
currentScale = 2.5
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
// 計算後の拡大率で、imageViewを拡大縮小する
|
266
|
+
|
267
|
+
getphoto.transform = CGAffineTransform(scaleX: currentScale, y: currentScale)
|
268
|
+
|
269
|
+
default:
|
270
|
+
|
271
|
+
// ピンチ中と同様だが、拡大率の範囲が異なる
|
272
|
+
|
273
|
+
if currentScale < 0.5 {
|
274
|
+
|
275
|
+
currentScale = 0.5
|
276
|
+
|
277
|
+
} else if currentScale > 2.0 {
|
278
|
+
|
279
|
+
currentScale = 2.0
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
// 拡大率が基準から外れている場合、指を離したときにアニメーションで拡大率を補正する
|
286
|
+
|
287
|
+
// 例えば指を離す前に拡大率が0.3だった場合、0.2秒かけて拡大率が0.5に変化する
|
288
|
+
|
289
|
+
UIView.animate(withDuration: 0.2, animations: {
|
290
|
+
|
291
|
+
self.getphoto.transform = CGAffineTransform(scaleX: self.currentScale, y: self.currentScale)
|
292
|
+
|
293
|
+
}, completion: nil)
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
//////////////////////////////////////////////////↑
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
func didReceiveMemoryWarning() {
|
308
|
+
|
309
|
+
super.didReceiveMemoryWarning()
|
310
|
+
|
311
|
+
// Dispose of any resources that can be recreated.
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
```
|
322
|
+
|
323
|
+
エラー箇所のみコメントを追加しています。
|
324
|
+
|
325
|
+
ご確認していただけると幸いです。よろしくお願いします。
|