7

We currently are using a solution similar to the one mentioned here (see Ares's answer). This does not seem to work in iOS8.

I have a form sheet, and I want to dismiss it as soon as the user taps on the dimmed view 'behind' the form sheet. Previously, this seemed possible by adding a gesture recogniser to the window, and checking tap-location to see if it was outside the current form sheet;

I also noticed the point needs to be converted (switch x and y) of the device is used in landscape mode. Other than that, right now it only receives gestures that occurred from inside the form sheet, where before any tap gesture anywhere on the screen would trigger an event.

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
    self.recognizer.numberOfTapsRequired = 1;
    self.recognizer.cancelsTouchesInView = NO;
    [self.view.window addGestureRecognizer:self.recognizer];
}

- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        CGPoint location = [sender locationInView:nil];
        if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) && IOS8)
        {
            location = CGPointMake(location.y, location.x);
        }

        // if tap outside pincode inputscreen
        BOOL inView = [self.view pointInside:[self.view convertPoint:location     fromView:self.view.window] withEvent:nil];
        if (!inView)
        {
            [self.view.window removeGestureRecognizer:sender];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}
Community
  • 1
  • 1
Kevin R
  • 8,230
  • 4
  • 41
  • 46

1 Answers1

9

As mentioned in the thread you referenced, you should add a UIGestureRecognizerDelegate and implement the methods:

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

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

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return YES;
}
Erich
  • 2,509
  • 2
  • 21
  • 24
  • I'm not sure what you mean? How does this reflect API changes in ios8? The code mentioned works fine in ios7. Or is this a way to circumvent a ios8 bug? – Kevin R Sep 15 '14 at 19:00
  • To make the code you posted work in iOS8, add a `UIGestureRecognizerDelegate` to your `self.recognizer` and add these delegate methods – Erich Sep 15 '14 at 19:03
  • 1
    Only to add: I was using nib files to create the ViewController. In that case on `viewDidLoad:` the `self.view.window` was nil, so I had to set the gesture recognizer on `viewDidAppear:` to make this work. – Tamara Bernad Jul 30 '15 at 07:41