按屏幕上的任意位置即可关闭UIActionSheet

时间:2013-02-02 02:14:12

标签: iphone uiactionsheet uitapgesturerecognizer

如何通过按UIActionsheet以外的屏幕上的任何位置来使UIActionSheet消失。这是我试图实现的目前无效的代码

- (void) viewDidAppear:(BOOL)animated
{
    [paintingVIew becomeFirstResponder];
    [super viewWillAppear:animated]; // or viewDidAppear?

    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];
}



 - (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];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

UIActionSheet是模态的,这意味着当显示操作表时,任何基础视图都不会接收到触摸(就像UIAlertView一样)。因此,要回答您的问题:您无法使用默认UIActionSheet

执行此操作