UIScrollview获取触摸事件

我如何检测我的UIScrollView触摸点? 触摸委托方法不起作用。

设置一个轻击手势识别器:

 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; [scrollView addGestureRecognizer:singleTap]; 

你会得到的触及:

 - (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture { CGPoint touchPoint=[gesture locationInView:scrollView]; } 

你可以创build你自己的UIScrollview子类,然后你可以实现以下function:

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"DEBUG: Touches began" ); UITouch *touch = [[event allTouches] anyObject]; [super touchesBegan:touches withEvent:event]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"DEBUG: Touches cancelled"); // Will be called if something happens - like the phone rings UITouch *touch = [[event allTouches] anyObject]; [super touchesCancelled:touches withEvent:event]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"DEBUG: Touches moved" ); UITouch *touch = [[event allTouches] anyObject]; [super touchesMoved:touches withEvent:event]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"DEBUG: Touches ending" ); //Get all the touches. NSSet *allTouches = [event allTouches]; //Number of touches on the screen switch ([allTouches count]) { case 1: { //Get the first touch. UITouch *touch = [[allTouches allObjects] objectAtIndex:0]; switch([touch tapCount]) { case 1://Single tap break; case 2://Double tap. break; } } break; } [super touchesEnded:touches withEvent:event]; } 

如果我们正在讨论scrollview中的点,那么你可以使用委托方法来挂钩:

 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 

并在方法内部读取属性:

 @property(nonatomic) CGPoint contentOffset 

从scrollView得到协调。