我有一个包含许多子视图的UIScrollView。其中一个子视图用于显示折线图,因此用户可能需要水平拖动。事实是,当我想水平拖动手指时,UIScrollView的垂直滚动很容易激活。现在,我想在chart的子视图中禁用垂直滚动,并在其余部分保持活动状态。
我尝试将UIPanGestureRecognizer添加到我的图表子视图中。它确实禁用了垂直滚动,但水平滚动也被禁用。我知道我可以在手势识别器的处理程序中编写代码来告诉我需要的垂直或水平滚动。但水平滚动实际上是由子视图的控制器管理的,该控制器是一个第三方库(具体而言是JBChartView)。我想知道是否有一个简单的方法来解决这个问题。
非常感谢。
发布于 2014-11-25 17:53:36
多亏了Dev和Astoria,我解决了这个问题。现在我想在这里发布我的解决方案,以防有人遇到和我一样的问题。
结果如下:

因为唯一水平视图不是scrollView (实际上它是一个JBLineChartView),所以最简单的scrollViewDidScroll方式也没有用。
我们仍然需要GestureRecognizer来实现这个目标,但就像Dev所说的,以一种更基本的方式,-touchesBegan方法。遗憾的是,UIScrollView不会响应这种触摸,我们需要为它编写一个类别,如下所示:
@implementation UIScrollView (UITouchEvent)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
self.scrollEnabled = YES;//This is the only line that you need to customize
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesMoved:touches withEvent:event];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesEnded:touches withEvent:event];
[super touchesEnded:touches withEvent:event];
}
@end哈,那它就起作用了!
BTW....This是我在StackOverflow上的第一个问题,这真的是一次很好的旅行。
发布于 2014-11-24 17:32:36
使用UIResponder类的-touchesBegan。如果触摸来自您的子视图(您不想滚动它)。禁用滚动视图。如果为该视图调用了-touchesEnded或-touchesCancelled。启用滚动视图。
发布于 2014-11-24 18:17:18
如果我没记错的话,您的主滚动视图包含另一个滚动视图,它是折线图的父视图。在这种情况下,您可以使用UIScrollViewDelegate协议方法。只需在开始滚动子视图时禁用主滚动视图即可。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if(scrollView==self.lineChartParent)
self.mainScrollView.scrollingEnabled = NO;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if(scrollView==self.lineChartParent)
self.mainScrollView.scrollingEnabled = YES;
}https://stackoverflow.com/questions/27100739
复制相似问题