嵌套的 uiscrollviews 和自定义处理事件路由
Nested uiscrollviews and custom handling event routing
我嵌套了 UIScrollView,使内部 UIScrollView 位于外部 UIScrollView 的第二页。 (即内部框架将是 CGRectMake(320, 0, view.width, view.height))。两个滚动视图只能水平移动。我只想在屏幕上滑动内部滚动视图(即外部滚动视图移动到第二页),直到内部滚动视图结束。一旦内部的到达尽头,那么我想识别外部的滑动。我首先尝试在第二页上设置 outerScrollView.scrollEnabled = NO
,但是内页也没有收到滑动手势。我还尝试将 UIScrollView 子类化并覆盖 hitTest:event
到 return 内部的 UIView,这也没有成功。有没有办法将滑动事件处理到特定视图并阻止其他视图?
尝试检测滚动视图何时到达内部视图的底部,方法是使用代码 here. If it's reached the bottom, flip a boolean (remember to flip it back if the user moves the scroll view away from the bottom!). If the boolean is flipped, use the code here to pass the touches to the superview and allow horizontal scrolling of the containing UIScrollView. If you're having trouble forcing the scroll try the code here 强制滚动。
编辑:
我只是写了一些代码来测试这个。通过限制每个视图可以滚动的方向,我能够让它按照您描述的方式工作,而无需使用我上面提到的大部分内容(更简单)。这是滚动视图的代码:
outer = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[outer setBackgroundColor:[UIColor redColor]];
[outer setShowsHorizontalScrollIndicator:true];
[outer setShowsVerticalScrollIndicator:false];
[outer setScrollEnabled:true];
[outer setDelegate:self];
[outer setAlwaysBounceHorizontal:true];
[outer setAlwaysBounceVertical:false];
outerContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width*2, self.view.bounds.size.height)];
[outerContent setBackgroundColor:[UIColor blueColor]];
[outer addSubview:outerContent];
outer.contentSize = outerContent.frame.size;
[self.view addSubview:outer];
inner = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[inner setBackgroundColor:[UIColor yellowColor]];
[inner setShowsHorizontalScrollIndicator:false];
[inner setShowsVerticalScrollIndicator:true];
[inner setAlwaysBounceHorizontal:false];
[inner setAlwaysBounceVertical:true];
[inner setScrollEnabled:true];
[inner setDelegate:self];
innerContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height*2)];
[innerContent setBackgroundColor:[UIColor greenColor]];
inner.contentSize = innerContent.frame.size;
[inner addSubview:innerContent];
[self.outerContent addSubview:inner];
我对视图进行了颜色编码,以便您了解发生了什么。 "inner" 和 "outer" 是我在头文件中声明并在 class 中合成的 UIScrollView。 "innerContent"和"outerContent"也是头文件中的UIView
我嵌套了 UIScrollView,使内部 UIScrollView 位于外部 UIScrollView 的第二页。 (即内部框架将是 CGRectMake(320, 0, view.width, view.height))。两个滚动视图只能水平移动。我只想在屏幕上滑动内部滚动视图(即外部滚动视图移动到第二页),直到内部滚动视图结束。一旦内部的到达尽头,那么我想识别外部的滑动。我首先尝试在第二页上设置 outerScrollView.scrollEnabled = NO
,但是内页也没有收到滑动手势。我还尝试将 UIScrollView 子类化并覆盖 hitTest:event
到 return 内部的 UIView,这也没有成功。有没有办法将滑动事件处理到特定视图并阻止其他视图?
尝试检测滚动视图何时到达内部视图的底部,方法是使用代码 here. If it's reached the bottom, flip a boolean (remember to flip it back if the user moves the scroll view away from the bottom!). If the boolean is flipped, use the code here to pass the touches to the superview and allow horizontal scrolling of the containing UIScrollView. If you're having trouble forcing the scroll try the code here 强制滚动。
编辑: 我只是写了一些代码来测试这个。通过限制每个视图可以滚动的方向,我能够让它按照您描述的方式工作,而无需使用我上面提到的大部分内容(更简单)。这是滚动视图的代码:
outer = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[outer setBackgroundColor:[UIColor redColor]];
[outer setShowsHorizontalScrollIndicator:true];
[outer setShowsVerticalScrollIndicator:false];
[outer setScrollEnabled:true];
[outer setDelegate:self];
[outer setAlwaysBounceHorizontal:true];
[outer setAlwaysBounceVertical:false];
outerContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width*2, self.view.bounds.size.height)];
[outerContent setBackgroundColor:[UIColor blueColor]];
[outer addSubview:outerContent];
outer.contentSize = outerContent.frame.size;
[self.view addSubview:outer];
inner = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[inner setBackgroundColor:[UIColor yellowColor]];
[inner setShowsHorizontalScrollIndicator:false];
[inner setShowsVerticalScrollIndicator:true];
[inner setAlwaysBounceHorizontal:false];
[inner setAlwaysBounceVertical:true];
[inner setScrollEnabled:true];
[inner setDelegate:self];
innerContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height*2)];
[innerContent setBackgroundColor:[UIColor greenColor]];
inner.contentSize = innerContent.frame.size;
[inner addSubview:innerContent];
[self.outerContent addSubview:inner];
我对视图进行了颜色编码,以便您了解发生了什么。 "inner" 和 "outer" 是我在头文件中声明并在 class 中合成的 UIScrollView。 "innerContent"和"outerContent"也是头文件中的UIView