文法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

touchesの中身

タップ数
NSinteger *tapCount = [[touches anyObject] tapCount];
タップ位置
CGPoint point = [[touches anyObject] locationInView:self.view];

ダブルタップ、ドラッグ検知


- (void)singleTap
{
   if (sigleTap) {
       NSLog(@"singleTap");
   }
}

- (void)doubleTap
{
 NSLog(@"doubleTap");
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   sigleTap = NO;
   moved = YES;
   UITouch *touch = [touches anyObject];
   CGPoint point = [touch locationInView:self.superview];
   NSLog(@"moved");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   if (moved) {
       moved = NO;
       return;
   }
   
   if ([[touches anyObject]tapCount]==1) {
       sigleTap = YES;
       [self performSelector:@selector(singleTap) withObject:nil afterDelay:0.3];
   } else {
       sigleTap = NO;
       [self doubleTap];
   }
   
}

UIGestureRecognizerを使う

以下を用いる
   UITapGestureRecognizer
   UIPinchGestureRecognizer
   UIRotationGestureRecognizer
   UISwipeGestureRecognizer
   UIPanGestureRecognizer
   UILongPressGestureRecognizer
実装は
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(hoge:)];
[view addGestureRecognizer:recognizer];
とするだけ。xibでも定義可能

呼ばれたメソッドには、引数を指定してやればUIGestureRecognizerは渡される。

最終更新:2011年12月21日 15:34