码迷,mamicode.com
首页 > 其他好文 > 详细

FormSheet式模态视图,点击模态视图外隐藏模态视图的方法

时间:2014-11-23 19:03:36      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   color   os   sp   for   on   cti   

#pragma -mark 点击模态视图外的空白处隐藏模态视图


- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@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([UIApplication sharedApplication].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 shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return YES;

}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

{

    return YES;

}


FormSheet式模态视图,点击模态视图外隐藏模态视图的方法

标签:style   io   ar   color   os   sp   for   on   cti   

原文地址:http://my.oschina.net/CreeveLiu/blog/347913

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!