HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在外部点击iOS 8上关闭模式表格表视图大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在试图解除外部轻击iOS 8上的模式表格表视图,没有运气,
我试过这个代码

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

但是它没有检测到外部视图点击,任何建议?

解决方法

在iOS 8中实际上有两个问题。首先,手势识别不会开始。

我通过添加UIGestureRecognizerDelegate协议和实现来解决这个问题

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

另外,别忘了注册代表

recognizer.delegate = self;

现在,手势识别器应该识别手势,并且将调用目标方法(handleTapBehind :)。

这是iOS 8中的第二个问题LOCATIOnInView:如果将nil作为视图传递,似乎不虑设备方向。相反,传递根视图是有效的。

这是我的目标代码,似乎适用于iOS 7.1和8.0:

if (sender.state == UIGestureRecognizerStateEnded) {
    UIView *rootView = self.view.window.rootViewController.view;
    CGPoint LOCATIOn = [sender LOCATIOnInView:rootView];
    if (![self.view poinTinside:[self.view convertPoint:LOCATIOn fromView:rootView] withEvent:nil]) {
        [self dismissviewControllerAnimated:YES completion:^{
            [self.view.window removeGestureRecognizer:sender];
        }];
    }
}

大佬总结

以上是大佬教程为你收集整理的在外部点击iOS 8上关闭模式表格表视图全部内容,希望文章能够帮你解决在外部点击iOS 8上关闭模式表格表视图所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。