FormSheet mode view, click the method of hiding the modal view outside the modal view, formsheet Mode
# Pragma-mark click a blank area outside the modal view to hide the modal View
-(Void) viewDidAppear :( BOOL) animated
{
[Super viewDidAppear: animated];
_ TapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: selfaction: @ selector (handleTapBehind :)];
[_ TapRecognizer setNumberOfTapsRequired: 1];
_ TapRecognizer. cancelsTouchesInView = NO; // So the user can still interact with controls in the modal view
[Self. view. window addGestureRecognizer: _ tapRecognizer];
[_ TapRecognizer setDelegate :( id <UIGestureRecognizerDelegate>) self];
}
-(Void) handleTapBehind :( UITapGestureRecognizer *) sender
{
If (sender. state = UIGestureRecognizerStateEnded ){
// Passing nil gives us coordinates in the window
CGPoint location = [sender locationInView: nil];
// Swap (x, y) on iOS 8 in landscape
If (SYSTEM_VERSION_MORE_THAN_8 ){
If (UIInterfaceOrientationIsLandscape ([UIApplicationsharedApplication]. statusBarOrientation )){
Location = CGPointMake (location. y, location. x );
}
}
// 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];
}
}
}
# Pragma mark-UIGestureRecognizer Delegate
-(BOOL) gestureRecognizerShouldBegin :( UIGestureRecognizer *) gestureRecognizer
{
Return YES;
}
-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer author :( UIGestureRecognizer *) otherGestureRecognizer
{
Return YES;
}
-(BOOL) gestureRecognizer :( UIGestureRecognizer *) gestureRecognizer shouldReceiveTouch :( UITouch *) touch
{
Return YES;
}
Reprinted: http://my.oschina.net/CreeveLiu/blog/347913