简体   繁体   English

iOS8:在外部点击时关闭表单

[英]iOS8: Dismiss form sheet on outside tap

We currently are using a solution similar to the one mentioned here (see Ares's answer). 我们目前正在使用类似于此处提到的解决方案(请参阅Ares的回答)。 This does not seem to work in iOS8. 这在iOS8中似乎不起作用。

I have a form sheet, and I want to dismiss it as soon as the user taps on the dimmed view 'behind' the form sheet. 我有一张表单,我想在用户点击表格后面的灰色视图时立即将其解雇。 Previously, this seemed possible by adding a gesture recogniser to the window, and checking tap-location to see if it was outside the current form sheet; 以前,这似乎可以通过向窗口添加手势识别器,并检查分接位置以查看它是否在当前表单之外;

I also noticed the point needs to be converted (switch x and y) of the device is used in landscape mode. 我还注意到需要转换的点(开关x和y)在横向模式下使用。 Other than that, right now it only receives gestures that occurred from inside the form sheet, where before any tap gesture anywhere on the screen would trigger an event. 除此之外,现在它只接收从表单内部发生的手势,在屏幕上任何位置的任何轻击手势触发事件之前。

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)];
    self.recognizer.numberOfTapsRequired = 1;
    self.recognizer.cancelsTouchesInView = NO;
    [self.view.window addGestureRecognizer:self.recognizer];
}

- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        CGPoint location = [sender locationInView:nil];
        if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) && IOS8)
        {
            location = CGPointMake(location.y, location.x);
        }

        // if tap outside pincode inputscreen
        BOOL inView = [self.view pointInside:[self.view convertPoint:location     fromView:self.view.window] withEvent:nil];
        if (!inView)
        {
            [self.view.window removeGestureRecognizer:sender];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}

As mentioned in the thread you referenced, you should add a UIGestureRecognizerDelegate and implement the methods: 如您引用的主题中所述,您应该添加UIGestureRecognizerDelegate并实现这些方法:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM