Iphone SDK 通过在 ipad 外部单击来解除 ipad 上的 Modal ViewControllers [英] Iphone SDK dismissing Modal ViewControllers on ipad by clicking outside of it
问题描述
当用户在模态视图之外点击时,我想关闭 FormSheetPresentation 模态视图控制器......我已经看到很多应用程序这样做(例如 ipad 上的 ebay),但我不知道如何因为下面的视图是当模态视图像这样显示时禁用触摸(他们是否将其呈现为弹出窗口?)...有人有任何建议吗?
I want to dismiss a FormSheetPresentation modal view controller when the user taps outside the modal view...I have seen a bunch of apps doing this (ebay on ipad for example) but i cant figure out how since the underneath views are disabled from touches when modal views are displayed like this (are they presenting it as a popover perhaps?)...anyone have any suggestions?
推荐答案
我迟到了一年,但这很简单.
I'm a year late, but this is pretty straightforward to do.
让您的模态视图控制器将手势识别器附加到视图的窗口:
Have your modal view controller attach a gesture recognizer to the view's window:
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
[recognizer setNumberOfTapsRequired:1];
recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
[self.view.window addGestureRecognizer:recognizer];
[recognizer release];
处理代码:
- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint location = [sender locationInView:nil]; //Passing nil gives us coordinates in the window
//Then we 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 dismissModalViewControllerAnimated:YES];
}
}
}
就是这样.该死的 HIG,这是一种有用且通常是直观的行为.
That's about it. HIG be damned, this is a useful and often intuitive behavior.
这篇关于Iphone SDK 通过在 ipad 外部单击来解除 ipad 上的 Modal ViewControllers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!