iPhone – UIScrollView …检测触摸的坐标

可能重复:
UIScrollview获取触摸事件

是否有可能在UIScrollView中检测到手指触摸的位置?

我的意思是,假设用户以这种方式使用他的手指:轻按和滚动,再次举起手指,轻敲和滚动等等。是否有可能知道与自身相关的轻敲发生的CG点。查看滚动器是在? 卷轴占据了整个自我。

谢谢。

你可以用手势识别器来完成。 为了检测单击位置,使用UITapGestureRecognizer

 UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)] autorelease]; [myScrollView addGestureRecognizer:tapRecognizer]; - (void)tapAction:(UITapGestureRecognizer*)sender{ CGPoint tapPoint = [sender locationInView:myScrollView]; CGPoint tapPointInView = [myScrollView convertPoint:tapPoint toView:self.view]; } 

要将tapPoint转换为tapPoint ,您可以在UIView类中使用convertPoint:toView:方法

看看touchesBegan:withEvent:你将得到一个UITouch的 NSSet,而UITouch包含一个locationInView:方法,该方法应该返回触摸的CGPoint。

您可以在视图中find位置,并将滚动添加到它。 现在,你的下一个问题是-(void)touchesBegan:touches:event不会被调用,因为事件将被发送到你的滚动视图。 这可以通过子类化你的UIScrollView来解决,并让scrollview把触发事件发送给下一个响应者(你的视图)。

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // Position of touch in view UITouch *touch = [[event allTouches] anyObject]; CGPoint touchPoint = [touch locationInView:self.view]; // Scroll view offset CGPoint offset = scrollView.contentOffset; // Result CGPoint scrollViewPoint = CGPointMake(touchPoint.x, touchPoint.y + offset.y); NSLog(@"Touch position in scroll view: %f %f", scrollViewPoint.x, scrollViewPoint.y); }