检测UiScrollView上的触摸位置

我需要从UIScrollView获取触摸位置。 但是当我为我的scrollview创build一个子类:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ NSArray *touchesArray = [touches allObjects]; UITouch *touch = (UITouch *)[touchesArray objectAtIndex:0]; CGPoint point = [touch locationInView:self]; self.position = point;} 

touchesBegan函数并不总是被调用。 如果我快速滑动, touchesBegan永远不会被调用,但是直接调用scrollViewDidScroll

我无法使用UIGesture (UITapGestureRecognizer, ... )因为用户不点击或滑动。

有没有其他的方法从UIScrollView获取位置?

编辑:

感谢ThXou: https ://stackoverflow.com/a/15763450/1752843

UIScrollView子类:

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ self.position = point; return self; } 

我看到了一个答案 ,我认为它可以解决你的问题。 看来,SDK不再将触摸处理方法传递给UIScrollView子类。 所以,你应该执行hitTest:将触摸传递给你的子类。

请参阅答案以获取更多信息。

要检测滚动视图中的触摸位置,请尝试使用此代码

 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; [scrollView addGestureRecognizer:singleTap]; - (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture { CGPoint touchPoint=[gesture locationInView:scrollView]; } 

您也可以使用scrollview.panGestureRecognizerlocationInView方法来获取视图中的触摸坐标。 这可以在任何scrollView的委托方法取决于您的要求。

 scrollView.delaysContentTouches = NO; 

这将使得touchesBegan和其他触摸事件将在scrollViewDidScroll之前被触发。

hitTest是一个黑客,不应该是首选的解决scheme。