FormSheet mode view, click the method of hiding the modal view outside the modal view, formsheet Mode

Source: Internet
Author: User

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

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.