Ipad项目中用到的UIModalPresentationFormSheet,点击阴影部分dismiss 当前presented的controller

本文介绍了在iPad项目中,如何处理UIModalPresentationFormSheet模态展示方式,使用户能点击控制器以外的阴影部分来dismiss当前显示的视图控制器。通过添加自定义手势识别器并重写相关代理方法,实现了点击阴影区域关闭控制器的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个文章学到的知识:

http://my.oschina.net/CreeveLiu/blog/347913?fromerr=Pg3JEnxF

背景:

  xcode:Ver 7.0           sdk:9.0

  1>IPAD项目

   2>在一个大的controller(mainViewController)中推出一个小的controller(aViewController).

   3>推出viewController的代码大意如下:

aViewController.modalPresentationStyle = UIModalPresentationFormSheet;

        aViewController.preferredContentSize = CGSizeMake(self.view.frame.size.width / 2,self.view.frame.size.height/ 2);

        [mainViewController presentViewController:aViewController animated:YES completion:nil];

效果如下:其中绿色的是aViewController , 白色的背景是mainViewController

-------------------------------------------------- 
需求:

想要点击绿色以外的阴影部分让这个controller dismiss掉。 

但是All uncovered areas are dimmed to prevent the user from interacting with them.

(所有不被覆盖的阴影部分是阻止用户交互的,也就是绿色以外的部分)

-------------------------------------------------- 
解决方法: 

给  被弹出的viewController  中加入以下方法(例子中的 aViewController

Link:    http://my.oschina.net/CreeveLiu/blog/347913?fromerr=Pg3JEnxF

以下内容是从链接中copy过来的,深拷贝哟(玩笑:))。怕以后链接失效。

- (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    _tapRecognizer = [[UITapGestureRecognizer allocinitWithTarget: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.windowwithEvent: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;

}

现在点击阴影部分就可以dismiss了。

-------------------------------------------------- 

总结:

不加手势的代理方法 是添加不上手势的,FromSheet样式可能是默认禁掉了手势的代理,所以重写一下就好用了。(因为用起来没什么bug,

所以这个原因没仔细研究,先暂时这么理解吧)

-------------------------------------------------- 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值