- UITapGestureRecognizer
- タップ操作を検知。タップ数(numberOfTapsRequired)や指の本数(numberOfTouchesRequired)を指定することができます。
- UILongPressGestureRecognizer
- 長押しを検知。認識されまるの時間(minimumPressDuration)を指定することができます。ロングプレスは長押しが検知されたときと指が離されたときにイベントが発生します。
- UISwipeGestureRecognizer
- スワイプを検知。スワイプの方向(direction)や指の本数(numberOfTouchesRequired)を指定することができます。
- UIPanGestureRecognizer
- パン(ドラッグ)を検知。ドラッグ開始から終了までの移動座業(translationInView)や移動の早さ(velocityInView)を取得することができます。
- UIPinchGestureRecognizer
- ピンチ操作を検知。倍率(scale)や早さ(velocity)を取得できます。
- UIRotateGestureRecognizer
- 回転操作を検知。回転角度(roration)や早さ(velocity)を取得できます。
// ジェスチャーの設定
- (void)createGestureRecognizer
{
// タップ検知
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
// 長押し検知
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleLongPressGesture:)];
longPressGesture.minimumPressDuration = 0.5f;
[self.view addGestureRecognizer:longPressGesture];
[longPressGesture release];
// スワイプ検知(Left)
UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleSwipeLeftGesture:)];
swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeftGesture];
[swipeLeftGesture release];
}
// タップ検知時
- (void)handleTapGesture:(UITapGestureRecognizer *)sender
{
[self showAlert:@"TapGesture"];
}
// 長押し検知時
- (void)handleLongPressGesture:(UILongPressGestureRecognizer *)sender
{
[self showAlert:@"LongPressGesture"];
}
// スワイプ(Left)
- (void)handleSwipeLeftGesture:(UISwipeGestureRecognizer *)sender
{
[self showAlert:@"SwipeLeftGesture"];
}
// アラート表示
- (void)showAlert:(NSString *)msg
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"確認ダイアログ" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
上のサンプルはすべてコード上でジェスチャーを生成していますがXcode 4.2以降ではインターフェースビルダーを利用してGesture Recognizerを設定できます。
操作はオブジェクトライブラリ内の各ジェスチャーを対象のオブジェクトの上にドラッグ&ドロップします。
状況を確認するにはアウトラインを表示して対象のジェスチャー(Pan Gesture Recognizer)を右クリックすると接続状況が確認できます。
次にViewController.hにアクション用のメソッドを追加します。
- (IBAction)panGesture:(UIPanGestureRecognizer *)recognizer;
次にAsisstantEditorを開いてPan Gesture Recognizerと上で追加したメソッドを関連付けます。
あとはViewController.mにメソッドの処理を記述するとGesture Recognizerの実装が可能となります。














