检测UIScrollView上的触摸位置?

the*_*tic 11 touch uiscrollview uiresponder uigesturerecognizer ios

我想在用户开始拖动时检测UIScrollView中的(初始)触摸位置.我已经搜索了这个问题,很多人似乎都在努力解决这个问题.现在,虽然我仍然不能理解为什么Apple不允许用户在滚动视图中访问触摸信息,但我不禁自己找到了解决方案.但是我的所有尝试都失败了,所以我想问你.

这是我认为会起作用的:

我在我的UIScrollView子类中设置了这样的UIPanGestureRecognizer,并将其添加到其手势识别器中:

UIPanGestureRecognizer *tapDrag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(touchedAndDragged:)];
tapDrag.cancelsTouchesInView = NO;
tapDrag.delegate = self;
[self addGestureRecognizer:tapDrag];  
Run Code Online (Sandbox Code Playgroud)

和相应的方法:

-(void)touchedAndDragged:(UIPanGestureRecognizer*)t{

     CGPoint loc = [t locationInView:self];
     //do something with location (that is exactly what I need)
     //...

     //Now DISABLE and forward touches to scroll view, so that it scrolls normally
     t.enabled = NO;
     /****
     ?????
     *****/

}
Run Code Online (Sandbox Code Playgroud)

如评论所示,我想在获得该点之后禁用平移手势,然后禁用识别器(同时仍然拖动!)并将触摸"传递"到我的滚动视图,以便用户可以正常滚动.那可行吗?还有其他解决方案吗?

Mic*_*lum 34

那么UIScrollView已经有了内置的平移手势,你可以利用它.用法就像将您的类设置为滚动视图的委托(利用scrollViewWillBeginDragging)和使用UIPanGestureRecognizer的-locationInView:来确定触摸位置一样简单.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    CGPoint location = [scrollView.panGestureRecognizer locationInView:scrollView];
    NSLog(@"%@",NSStringFromCGPoint(location));
}
Run Code Online (Sandbox Code Playgroud)