ピンチ(二本指で操作、縮小、拡大)イベントの設定(イベント) UIPinchGestureRecognizer
objective-c サンプル
iPhone の開発で、二本指の操作でビューの拡大、縮小、画像の拡大、縮小を処理するイベントを実装する方法です。

ピンチ前 −−−−−−−−−−−−−−−−−−−−> ピンチ後
UIPinchGestureRecognizer をインスタンス化します。また、イベント発生時に呼び出すメソッドを selector で指定します。
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePinchGesture:)];
Viewへ関連付けします。
[self.view addGestureRecognizer:pinchGesture];
以下が、ピンチされた時に呼び出されるメソッドになります。上記の selector にて設定をしています。
拡大、縮小の動作を2本指で操作している間ずっとイベントが発生します。
– (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender {
ピンチによって拡大、縮小したスケールを取得します。
CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
上記で取得したスケール率をビューにセットしてビューを縮小拡大させます。
self.view.transform = CGAffineTransformMakeScale(factor, factor);
コード サンプル
// Pinching in and out (for zooming a view)
// UIPinchGestureRecognizer
// ピンチ
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];
// セレクター
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender {
CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
self.view.transform = CGAffineTransformMakeScale(factor, factor);
NSLog(@"factor %f",factor);
}
