如何将UITapGestureRecognizer添加到presentViewController并进行查看

时间:2014-08-25 10:44:46

标签: ios xcode uiview presentmodalviewcontroller

我需要在点击控制器外部时解除presentViewController,我正在使用以下代码

   UITapGestureRecognizer *tapBehindGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBehindDetected:)];
  [tapBehindGesture setNumberOfTapsRequired:1];
  [tapBehindGesture setCancelsTouchesInView:NO];
  [self.view.window addGestureRecognizer:tapBehindGesture];

- (void)tapBehindDetected:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window

        //Then we convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view.

        if (![self.view pointInside:[self.view convertPoint:location fromView:self.view.window] withEvent:nil])
        {
            // Remove the recognizer first so it's view.window is valid.
            [self.view.window removeGestureRecognizer:sender];
            [self dismissViewControllerAnimated:YES completion:nil];
        } 
    }
}

它对我来说很好,但我在另一种观点中遇到了问题。在视图控制器内部,我将UITapGestureRecognizer添加到vwHeaderview。

     UITapGestureRecognizer *addNewContactsingleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        [vwHeaderview addGestureRecognizer:addNewContactsingleFingerTap];

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer
{
    //Some code
}

如果我点击vwHeaderview它调用“tapBehindDetected”,我就不会调用“handleSingleTap”。

如果我在上面评论“tapBehindGesture”它的工作正常。但我需要两个都工作。任何帮助

1 个答案:

答案 0 :(得分:0)

我将代理添加到vwHeaderview并使用以下功能&它对我来说很好......

UITapGestureRecognizer *addNewContactsingleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    addNewContactsingleFingerTap.delegate = self;
    [vwHeaderview addGestureRecognizer:addNewContactsingleFingerTap];

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {        
    return YES;
}