这个文章学到的知识:
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];
--------------------------------------------------
需求:
想要点击绿色以外的阴影部分让这个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 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;
}
现在点击阴影部分就可以dismiss了。
--------------------------------------------------
总结:
不加手势的代理方法 是添加不上手势的,FromSheet样式可能是默认禁掉了手势的代理,所以重写一下就好用了。(因为用起来没什么bug,
所以这个原因没仔细研究,先暂时这么理解吧)
--------------------------------------------------