Detect touch event on UIScrollView AND on UIView#39;s components [which is placed inside UIScrollView](在 UIScrollView 和 UIView 的组件上检测触摸事件 [放置在 UIScrollView 内])
问题描述
我的简单故事板 IPad 项目中有 UIViewController,其中包含放置在整个表面 (1024 x 768) 上的 UIScrollView.我创建了 3 个 XIB 文件,它们是我的应用程序在 viewDidLoad 启动时加载的 UIView,并将它们添加到 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 个相同的 UIView)
~~ UIScrollView (contains 3 identical UIViews)
~~~ UIView(UIViewClass 是这个 XIB 文件的 File's Owner)
~~~ UIView (UIViewClass is File's Owner for this XIB file)
~~~~ UIButton
~~~~ UIButton
我希望我的 UIViewControllerClass 能够同时意识到:触摸 UIScrollView 组件上的任意位置,以及如果 UIScrollView 被触摸,如果 UIScrollView 中的 UIView 内的 UIButton 被触摸,以获取该按钮被触摸的确切信息.
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)上设置 User Interaction Enabled = 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 组件上的触摸事件,但是不再调用 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 组件
- 在 UIScrollView 内的 UIView 中触摸 UIButton(如果已触摸此按钮)
我认为将触摸事件识别器附加到整个 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 选项设置为 NO!
Tap gesture must have cancelsTouchesInView option set to NO!
对于我上面的例子,这条线解决了所有问题:
For my case above, this line solves everything:
touch.cancelsTouchesInView = NO;
非常感谢郑.
推荐答案
我不知道这是否适合你,但我在这里给出了关于滚动视图内视图的触摸事件的答案:
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 中的键盘
这个想法是告诉滚动视图不要吞噬滚动视图区域内的所有点击手势.
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 内]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 UIScrollView 和 UIView 的组件上检测触摸事件 [放
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01