如何将触摸事件传递给 parentViewController?
Posted
技术标签:
【中文标题】如何将触摸事件传递给 parentViewController?【英文标题】:How do I pass touch events through to parentViewController? 【发布时间】:2013-03-21 19:03:17 【问题描述】:我有一个带有几个容器(子)UIiewsController 的 UIViewContoller。主 UIViewController (mvc) 包含:
1.) 占据整个 mvc 视图的 UICollectionView(它在 mvc.view 之上,但在所有其他控件之下)。 2.) 显示搜索选项的 UIViewController (s1vc) 3.) 另一个类似于 #2 (s2vc) 4.) 另一个类似于 #2 (s3vc)我在 mvc 中添加了手势识别器,以便用户可以通过将每个子视图控制器从屏幕上滑出来隐藏/显示它们。
问题是当用户将任何 svcs 滑出屏幕时,他们无法滚动 mvc 的 collectionView。
这是我隐藏/显示 svcs 的方式:
-(void)swipeLeftGestureHandler:(UIGestureRecognizer*)gestureRecognizer
SMLOG(@"Swiped Left");
if([SMUser activeUser] == nil) return;
if([self gestureHorizontalScreenSide:gestureRecognizer] == kHorizontalScreenSideLeft)
[self hideFacets];
else
[self showAccordion];
-(void)swipeRightGestureHandler:(UIGestureRecognizer*)gestureRecognizer
SMLOG(@"Swiped Right");
if([SMUser activeUser] == nil) return;
if([self gestureHorizontalScreenSide:gestureRecognizer] == kHorizontalScreenSideLeft)
[self showFacets];
else
[self hideAccordion];
-(void)hideFacets
if(self.facetVisible == NO) return;
[UIView animateWithDuration:0.25
animations:^
CGRect newFrame = self.facetViewController.view.frame;
newFrame.origin = CGPointMake(newFrame.origin.x - newFrame.size.width, newFrame.origin.y);
self.facetViewController.view.frame = newFrame;
self.facetVisible = NO;
completion:^(BOOL finished)
self.facetViewController.view.hidden = YES;
self.facetViewController.view.userInteractionEnabled = NO;
];
-(void)showFacets
if([SMUser activeUser] == nil) return;
if(self.facetVisible == YES) return;
self.facetViewController.view.userInteractionEnabled = YES;
[UIView animateWithDuration:0.25
animations:^
self.facetViewController.view.hidden = NO;
CGRect newFrame = self.facetViewController.view.frame;
newFrame.origin = CGPointMake(newFrame.origin.x + newFrame.size.width, newFrame.origin.y);
self.facetViewController.view.frame = newFrame;
self.facetVisible = YES;
completion:^(BOOL finished)
];
如您所见,我正在切换 svc.view.hidden 属性,然后我也尝试切换 svc.userInteractionEnabled.property 但没有运气。仍然无法通过滑动构面视图控制器所在的位置来滑动集合视图。
有什么想法吗?
【问题讨论】:
【参考方案1】:这里的解决方案是在 parentViewController 中为容器视图添加另一个插座(使用 IB)(我在此代码中将其称为 facetContainerView),然后将其设置为 userInteractionEnabled 属性。
-(void)hideFacets
if(self.facetVisible == NO) return;
self.facetVisible = NO;
[UIView animateWithDuration:0.25
animations:^
CGRect newFrame = self.facetViewController.view.frame;
newFrame.origin = CGPointMake(newFrame.origin.x - newFrame.size.width, newFrame.origin.y);
self.facetViewController.view.frame = newFrame;
completion:^(BOOL finished)
self.facetViewController.view.hidden = YES;
self.facetContainerView.userInteractionEnabled = NO;
];
-(void)showFacets
if(self.facetVisible == YES) return;
self.facetVisible = YES;
self.facetContainerView.userInteractionEnabled = YES;
[UIView animateWithDuration:0.25
animations:^
self.facetViewController.view.hidden = NO;
CGRect newFrame = self.facetViewController.view.frame;
newFrame.origin = CGPointMake(newFrame.origin.x + newFrame.size.width, newFrame.origin.y);
self.facetViewController.view.frame = newFrame;
completion:^(BOOL finished)
];
我很好奇这个新的视图出口与 self.facetViewController.view 有何不同,所以我插入了这段代码来比较地址(它们确实不同)。我不确定层次结构,但似乎还有一个我不知道的额外视图层。
NSLog(@"self.facetViewController.view: %p", self.facetViewController.view);
NSLog(@"self.facetContainerView: %p", self.facetContainerView);
希望这会在某个时候对某人有所帮助。
【讨论】:
以上是关于如何将触摸事件传递给 parentViewController?的主要内容,如果未能解决你的问题,请参考以下文章
userInteractionEnabled = YES时如何将触摸事件传递给superview?