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

时间:2014-09-02 13:07:58

标签: ios objective-c xcode ios8

我们目前正在使用类似于here提及的解决方案(参见Ares的回答)。这在iOS8中似乎不起作用。

我有一张表格,我想在用户点击暗淡的视图后立即将其解雇。表单。 以前,这似乎可以通过向窗口添加手势识别器,并检查分接位置以查看它是否在当前表单之外;

我还注意到需要转换的点(开关x和y)在横向模式下使用。除此之外,它现在只接收从表单内部发生的手势,在屏幕上任何位置的任何点击手势触发事件之前。

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

1 个答案:

答案 0 :(得分:10)

正如您引用的主题中所提到的,您应该添加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;
}