检测UIScrollView上的触摸事件和UIView的组件[放在UIScrollView中] [英] Detect touch event on UIScrollView AND on UIView's components [which is placed inside UIScrollView]

查看:97
本文介绍了检测UIScrollView上的触摸事件和UIView的组件[放在UIScrollView中]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的简单故事板IPad项目上有UIViewController,它包含放置在整个表面(1024 x 768)上的UIScrollView。我创建了3个XIB文件,这些文件是我的应用程序在viewDidLoad中启动时加载的UIViews,并将它们添加到UIScrollView中。这3个XIB文件中的每一个只包含一个UIButton。

I have UIViewController on my simple storyboard IPad project which contains UIScrollView which is placed over entire surface (1024 x 768). I have created 3 XIB files which are UIViews which my application loads on start in viewDidLoad and add them into UIScrollView. Each of these 3 XIB files contains only one UIButton.

这是层次结构:


~UIViewController( UIViewControllerClass 是这个
UIViewController的类)

~ UIViewController (UIViewControllerClass is class for this UIViewController)

~~ UIScrollView(包含3个相同的UIViews)

~~ UIScrollView (contains 3 identical UIViews)

~~~ UIView( UIViewClass 是此XIB文件的文件所有者)

~~~ UIView (UIViewClass is File's Owner for this XIB file)

~~ ~~ UIButton

~~~~ UIButton

我希望我的UIViewControllerClass能够识别两者:触摸UIScrollView组件上的任何地方并触摸UIScrollView,如果UIButton触摸了UIScrollView中UIView的内部,以获得确切触摸该按钮的信息。

I would like that my UIViewControllerClass becomes aware of both: touch anywhere on UIScrollView component AND if UIScrollView is touched, if UIButton inside of UIView in UIScrollView is touched, to have information that exactly that button is touched.

我在UIViewClass中创建了IBAction,用于触摸UIScrollView中UIView内的UIButton以及当我设置时用户交互在所有元素(UIViewController,UIView和UIScrollView)上启用= YES,此方法应该被调用。

I made IBAction inside UIViewClass for touch on UIButton inside UIView in UIScrollView and when I set User Interaction Enabled = YES on all elements (UIViewController, UIView and UIScrollView) this method is called as it should.

但此时我的UIViewControllerClass并不知道在UIButton上的UIScrollView内发生了触摸。我做了这样的触摸识别器:

But at this point my UIViewControllerClass isn't aware that touch occurred inside UIScrollView on that UIButton. I made touch recognizer like this:

UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouch)];
touch.numberOfTouchesRequired = 1;

并将其添加到UIScrollView组件。通过这种方式,我能够在UIViewControllerClass中检测UIScrollView组件上的触摸事件,但是UIView中的UIButton触摸事件处理程序不再被调用。

and added it to UIScrollView component. In this way I am able to detect touch event on UIScrollView component in UIViewControllerClass, but touch event handler for UIButton in UIView which is inside UIScrollView isn't called anymore.

所以,我需要在UIViewControllerClass中有这两个信息:

So, I need to have these two informations in UIViewControllerClass:


  • 触摸UIScrollView组件

  • 触摸UIVut中的UIButton,它位于UIScrollView内部(如果触摸了此按钮)

我认为将触摸事件识别器附加到整个UIScrollView组件不是解决方案,因为它禁用了我在UIViewClass中编写的所有触摸事件处理程序。

I suppose that attaching touch event recognizer to entire UIScrollView component isn't solution, since it disables all touch event handlers I wrote inside UIViewClass.

我认为解决方案是以某种方式触及UIScrollView中UIView中的组件应该发送到UIViewControllerClass,但我没有找到办法做到这一点。

I think solution is that somehow touches which are made on components in UIView inside UIScrollView should be sent up to UIViewControllerClass, but I didn't found a way to do this.

如果有人可以帮助我,我将非常感激。提前致谢。

If anyone can help me, I'd be very grateful. Thanks in advance.

点击手势必须将 cancelsTouchesInView 选项设置为

Tap gesture must have cancelsTouchesInView option set to NO!

对于我的上述情况,这一行解决了所有问题:

For my case above, this line solves everything:

touch.cancelsTouchesInView = NO;

非常感谢郑。

推荐答案

我不知道这是否适合你,但我已经在这里给出了关于scrollview中视图的触摸事件的答案:

I don't know if this works for you or not, but I've given an answer about touch events for views inside scrollview here:

在UIScrollView中解除键盘

这个想法是告诉scrollView不要吞下滚动视图区域内的所有点击手势。

The idea is to tell the scrollView not to swallow up all tap gestures within the scroll view area.

我会无论如何粘贴代码,希望它能解决你的问题:

I'll paste the code here anyways, hopefully it fixes your problem:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];

// prevents the scroll view from swallowing up the touch event of child buttons
tapGesture.cancelsTouchesInView = NO;    

[pageScrollView addGestureRecognizer:tapGesture];

[tapGesture release];

...

// method to hide keyboard when user taps on a scrollview
-(void)hideKeyboard
{
    [myTextFieldInScrollView resignFirstResponder];
}

这篇关于检测UIScrollView上的触摸事件和UIView的组件[放在UIScrollView中]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆