2013-06-27 108 views
10

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

这里是我想会的工作:

我成立了一个UIPanGestureRecognizer像这样在我的UIScrollView子类,并将其添加到它的手势识别:

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

和相应的方法:

-(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; 
    /**** 
    ????? 
    *****/ 

} 

正如评论所示,我想禁用平移手势后,我有点,然后禁用识别器(而仍然拖动!)并将触摸“传递”到我的滚动视图,以便用户可以正常滚动。这是否可行?还有其他解决方案吗?

回答

31

那么UIScrollView已经有一个内置的平移手势,你可以利用。用法与将滚动视图的委托设置为(使用scrollViewWillBeginDragging)并使用UIPanGestureRecognizer的-locationInView:来确定触摸位置一样简单。

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{ 
    CGPoint location = [scrollView.panGestureRecognizer locationInView:scrollView]; 
    NSLog(@"%@",NSStringFromCGPoint(location)); 
} 
+0

是否有可能检测出这个滚动是否在某个xx位置,然后scrollView滚动或触摸将不会被触发?假设我在滚动视图中有两个视图,一个地图和一个面板在地图下方,我不希望滚动视图在我触摸地图时触发其代表,并且地图应该触及它 –

0

为什么不抓住-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event的起始位置,这将更加方便和高效。

+3

我完全意识到触摸功能,事实上,我试图实现它们,但它们根本不会被调用。 –

+0

未启用用户交互。但禁用它,它会。但是,那么你将无法与其任何childobjects进行交互... – Philip