如何将触摸事件传递给parentViewController?

时间:2013-03-21 19:03:18

标签: iphone ios ipad uiviewcontroller uiviewanimation

我有一个带有几个容器(子)UIiewsController的UIViewContoller。主UIViewController(mvc)包含:

  • 1。)占用整个mvc视图的UICollectionView(它高于mvc.view但低于所有其他控件)。
  • 2。)显示搜索选项(s1vc)
  • 的UIViewController
  • 3.)另一个类似于#2(s2vc)
  • 4。)另一个类似于#2(s3vc)

enter image description here

我已经为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 个答案:

答案 0 :(得分:0)

这里的解决方案是在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有何不同,所以我插入这个代码来比较地址(它们确实不同)。我不确定heirarchy,但似乎有一个我不知道的额外视图层。

NSLog(@"self.facetViewController.view: %p", self.facetViewController.view);
NSLog(@"self.facetContainerView: %p", self.facetContainerView);

希望这会在某些时候帮助某人。