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

前端之家收集整理的这篇文章主要介绍了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];
        }
    }
}

解决方法

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

猜你在找的iOS相关文章