通过按屏幕上的任意位置来获取要关闭的UIActionSheet

通过按屏幕上的任意位置来获取要关闭的UIActionSheet

本文介绍了通过按屏幕上的任意位置来获取要关闭的UIActionSheet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过按屏幕上除UIAction工作表之外的任何位置来使UIActionSheet消失.这是我尝试实施的代码目前无法正常运行

How do i get a UIActionSheet to disapear by pressing anywhere on the screen other than the UIAction sheet. Heres the code i tried implementing thats not working currently

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

推荐答案

UIActionSheet 是模态的,这意味着在显示操作表时,任何基础视图都不会受到触摸(就像 UIAlertView ).因此,要回答您的问题:您不能使用默认的 UIActionSheet .

A UIActionSheet is modal, meaning that any underlying views will not receive touches when the action sheet is shown (just like a UIAlertView). So, to answer your question: you can't do this with the default UIActionSheet.

这篇关于通过按屏幕上的任意位置来获取要关闭的UIActionSheet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:20