문제

All I want to know is how to I recognise when a user taps outside of the modal dialog. I have tried this but it is not being called when the user taps outside.

Here is my viewDidLoad method which resides in the ModalDialogViewController.m file UITapGestureRecognizer *recognizer;

        if(![self.view.window.gestureRecognizers containsObject:recognizer])
        {
            recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
            //[recognizer setDelegate:self];
            [recognizer setNumberOfTapsRequired:1];
            recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
            [self.view.window addGestureRecognizer:recognizer];

        }

This is not opening the handleTapBehind method.

I have made the modal view controller a protocol of UIGestureRecognition.

도움이 되었습니까?

해결책

A bit late here, but just incase someone comes here by way of Google:

Setting up the gesture recognizer should happen after the view appears. During the viewDidLoad method invocation, the view's window is nil, so the gesture recognizer never gets added.

Put the method call in viewDidAppear and it should work as expected.

다른 팁

Have you set UIGestureRecognizerDelegate in you .h file?

You can get full information of behavior of UIGestureRecognition in Apple doc

Using UIGestureRecognizers is extremely simple. You just perform the following steps: Create a gesture recognizer. When you create a gesture recognizer, you specify a callback method so the gesture recognizer can send you updates when the gesture starts, changes, or ends.

Add the gesture recognizer to a view. Each gesture recognizer is associated with one (and only one) view. When a touch occurs within the bounds of that view, the gesture recognizer will look to see if it matches the type of touch it’s looking for, and if a match is found it will notify the callback method.

You can perform these two steps programatically , but it’s even easier adding a gesture recognizer visually with the Storyboard editor.

See Full tutorial and working here :

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top