UITapGestureRecognizer

UITapGestureRecognizer

本文介绍了UIPageViewController 捕获所有 UITapGestureRecognizer 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在键盘上度过了漫长的一天,所以我伸出手:-)

It's been a long day at the keyboard so I'm reaching out :-)

我在一个典型的实现中有一个 UIPageViewController,它基本上遵循 Apple 的标准模板.我正在尝试添加一个叠加层,允许用户执行诸如触摸按钮以跳转到某些页面或关闭视图控制器以转到应用程序的另一部分之类的操作.

I have a UIPageViewController in a typical implementation that basically follows Apple's standard template. I am trying to add an overlay that will allow the user to do things like touch a button to jump to certain pages or dismiss the view controller to go to another part of the app.

我的问题是 UIPageViewController 正在从我的叠加子视图中捕获所有事件,我正在努力寻找可行的解决方案.

My problem is that the UIPageViewController is trapping all events from my overlay subview and I am struggling to find a workable solution.

这是一些帮助示例的代码...

Here's some code to help the example...

在 viewDidLoad

// Page creation, pageViewController creation etc....

self.pageViewController.delegate = self;

[self.pageViewController setViewControllers:pagesArray
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:NO
                                completion:NULL];

self.pageViewController.dataSource = self;

[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];

// self.overlay being the overlay view

if (!self.overlay)
{
   self.overlay = [[MyOverlayClass alloc] init];  // Gets frame etc from class init
   [self.view addSubview:self.overlay];
}

这一切都很好.叠加层被创建,正如你所期望的那样,它会显示在 UIPageViewController 的页面顶部.当页面翻转时,它们会在覆盖层下方翻转 - 再次如您所料.

This all works great. The overlay gets created, it gets show over the top of the pages of the UIPageViewController as you would expect. When pages flip, they flip underneath the overlay - again just as you would expect.

然而,self.overlay 视图中的 UIButtons 永远不会获得点击事件.UIPageViewController 响应所有事件.

However, the UIButtons within the self.overlay view never get the tap events. The UIPageViewController responds to all events.

我尝试根据此处的建议覆盖 -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch ,但没有成功.

I have tried overriding -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch per the suggestions here without success.

UIPageViewController 手势识别器

我已经尝试手动捕获所有事件并自己处理它们 - 不起作用(老实说,即使这样做了,它看起来也有点像黑客).

I have tried manually trapping all events and handling them myself - doesn't work (and to be honest even if it did it would seem like a bit of a hack).

有没有人有关于如何捕获事件的建议,或者可能有更好的方法在 UIPageViewController 的顶部使用叠加层.

Does anyone have a suggestion on how to trap the events or maybe a better approach to using an overlay over the top of the UIPageViewController.

非常感谢所有帮助!!

推荐答案

尝试遍历 UIPageViewController.GestureRecognizers 并指定 self 作为这些手势的代理并实现

Try to iterate through UIPageViewController.GestureRecognizers and assign self as a delegate for those gesture and implement

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;

你的代码可能是这样的:

Your code may be like this:

在视图中DidLoad

In viewDidLoad

for (UIGestureRecognizer * gesRecog in self.pageViewController.gestureRecognizers)
{
        gesRecog.delegate = self;
}

并添加以下方法:

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (touch.view != self.pageViewController.view]
    {
        return NO;
    }
    return YES;
}

这篇关于UIPageViewController 捕获所有 UITapGestureRecognizer 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 03:59