[iPhone/iPad] UIGestureRecognizer でジェスチャーを識別する
iOS3.2 で追加された UIGestureRecognizer を使うと様々なジェスチャーを簡単に識別することができます。ただし、iOS3.2 以降で利用可能なので使用する際には注意が必要です。iPad アプリなら問題ないです。識別できるジェスチャーは以下のとおりです。
- タップ
- ピンチ
- パン(ドラッグ)
- スワイプ
- ローテイト
- ロングプレス
ローテイトは2本指でタッチして回転させることです。1本指でタッチして回転させてもパン(ドラッグ)としてしか認識されません。
使い方はそれぞれのインスタンスを作成して、ジェスチャーを認識したときに呼ぶアクションを指定して、そのアクションでジェスチャー認識時の処理を行います。
タップ
numberOfTapsRequired に認識するタップ数を指定します。デフォルトは1です。
// シングルタップ
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
// ダブルタップ
UITapGestureRecognizer* doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapGesture:)];
doubleTapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTapGesture];
[doubleTapGesture release];
- (void) handleTapGesture:(UITapGestureRecognizer*)sender {
NSLog(@"tap");
}
- (void) handleDoubleTapGesture:(UITapGestureRecognizer*)sender {
NSLog(@"double tap");
}
ピンチ
ピンチジェスチャー認識時に scale プロパティに拡大縮小率、velocity プロパティにピンチのスピードが入ります。
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];
[pinchGesture release];
- (void) handlePinchGesture:(UIPinchGestureRecognizer*) sender {
UIPinchGestureRecognizer* pinch = (UIPinchGestureRecognizer*)sender;
NSLog(@"pinch scale=%f, velocity=%f", pinch.scale, pinch.velocity);
}
パン(ドラッグ)
パンジェスチャー認識後に translationInView: で移動後の相対位置が取得できます。
UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[self.view addGestureRecognizer:panGesture];
[panGesture release];
- (void) handlePanGesture:(UIPanGestureRecognizer*) sender {
UIPanGestureRecognizer* pan = (UIPanGestureRecognizer*) sender;
CGPoint location = [pan translationInView:self.view];
NSLog(@"pan x=%f, y=%f", location.x, location.y);
}
スワイプ
direction プロパティで認識させる方向(上下左右)を指定します。デフォルトは右です。numberOfTouchesRequired プロパティで認識させるのに必要なタッチ数を指定できます。
// 左へスワイプ
UISwipeGestureRecognizer* swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeftGesture:)];
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeftGesture];
[swipeLeftGesture release];
// 右へスワイプ
UISwipeGestureRecognizer* swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRightGesture:)];
swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRightGesture];
[swipeRightGesture release];
- (void) handleSwipeLeftGesture:(UISwipeGestureRecognizer *)sender {
NSLog(@"swipe left");
}
- (void) handleSwipeRightGesture:(UISwipeGestureRecognizer *)sender {
NSLog(@"swipe right");
}
ローテイト
2本指を回転させた(ひねった)時に認識されます。認識後、rotation プロパティで回転した角度(ラジアン)とスピード(ラジアン/秒)が取得できます。
UIRotationGestureRecognizer* rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];
[rotationGesture release];
- (void) handleRotationGesture:(UIRotationGestureRecognizer*) sender {
UIRotationGestureRecognizer* rotation = (UIRotationGestureRecognizer*) sender;
NSLog(@"rotation rad=%f, velocity=%f", rotation.rotation, rotation.velocity);
}
ロングプレス
長押ししたときに認識されます。minimumPressDuration プロパティで認識されるまでの秒数を指定できます。デフォルトは 0.4 秒です。allowableMovement プロパティで長押ししている最中に動いても許容されるピクセル数を指定できます。デフォルトは 10 ピクセルです。注意点はロングプレスが認識されたときと指が話されたときに指定のアクションが呼ばれます。
UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
[self.view addGestureRecognizer:longPressGesture];
[longPressGesture release];
- (void) handleLongPressGesture:(UILongPressGestureRecognizer*) sender {
NSLog(@"long press");
}
関連する投稿
11 comments
コメントをどうぞ
Additional comments powered by BackType
[blog] [iPhone/iPad] UIGestureRecognizer でジェスチャーを識別する http://is.gd/f699w
This comment was originally posted on Twitter
Sun Limited Mt. – [iPhone/iPad] UIGestureRecognizer でジェスチャーを識別する http://www.syuhari.jp/blog/archives/2234
This comment was originally posted on Twitter
ダブルタップ、スワイプなどを検出。3.2以降で使用可能。 / [iPhone/iPad] UIGestureRecognizer でジェスチャーを識別する | Sun Limited Mt. http://htn.to/qvdBWr
This comment was originally posted on Twitter
メモ!! RT @tototti: ダブルタップ、スワイプなどを検出。3.2以降で使用可能。 / [iPhone/iPad] UIGestureRecognizer でジェスチャーを識別する | Sun Limited Mt. http://htn.to/qvdBWr
This comment was originally posted on Twitter
[iPhone/iPad] UIGestureRecognizer でジェスチャーを識別する | Sun Limited Mt.: [iPhone/iPad] UIGestureRecognizer でジェスチャーを識別する |… http://bit.ly/95rSGk
This comment was originally posted on Twitter
[iPhone/iPad] UIGestureRecognizer でジェスチャーを識別する | Sun Limited Mt. – http://is.gd/fbtd2
This comment was originally posted on Twitter
[...] 【参考】[iPhone/iPad] UIGestureRecognizer でジェスチャーを識別する | Sun Limited Mt.. タグ: iPhoneアプリ, objective-c, UITableView, xcode, ジェスチャー ショートURL:http://shinofara.com/s/ODI5Z {lang: 'ja'} Tweet Did you enjoy this article? Share it! [...]
[...] ロングプレス(長押し) 参考: UIGestureRecognizer でジェスチャーを識別する [...]
[...] [iPhone/iPad] UIGestureRecognizer でジェスチャーを識別する [...]
[...] UIGestureRecognizer [...]
3wrapper…
…