UIScrollview 和子视图触摸事件管理
我有主要的wiev。我已放置一个 UIScrollView 对象作为主视图的子视图。 我有一个来自 UIView 的扩展类,它管理触摸事件。此类模拟了一种透明黑板,用户可以在其中绘制路径。 我从 UIVIew 扩展类创建一个对象,并将其添加为 UIScrollView 的子视图。
我使用 Inspector 和 .xib 文件执行此过程。
然后,没有触摸事件到达 UIScrollView 对象。无论我是否在检查器中取消选中 UIView 扩展类的“用户交互已启用”复选框。 我还尝试过
[[self nextResponder] touchesBegan:touches withevent:event];
使用 : or
[[self.superview nextResponder] touchesBegan:touches withevent:event];
or
[super touchesBegan:touches withEvent:event];
or
[super touchesBegan:touches withEvent:event];
在扩展 UIView 类的 touchEvents 方法中 。 我没有成功地将触摸事件发送到 UIScrollView。
另一方面,如果我以编程方式添加scrollView和扩展的UIView类,并保持相同的层次结构,则UIScrollView会获取触摸事件,但不会获取其子视图(我的UIView扩展类)。
我不希望 UIScrollView 和 UIView 扩展类同时管理触摸事件。我只想在 UIScrollView 和 UIView 扩展类之间切换以监听触摸事件,例如使用按钮。
在本例中,我正在解释是否可以选择 UIScrollView 或我的 UIView 扩展类应该监听触摸事件?
I have the main wiev. I have placed a UIScrollView object as a subview of the main view.
I have an extended class from UIView, which manage touch events. This class models a sort of transparent blackboard, in which the user can draw a path.
I create an object out of my UIVIew extended class and I add it as a subview of the UIScrollView.
I carry out this process by using the Inspector and .xib file.
Then, no touch event gets to the UIScrollView object. No matter if I uncheck "User Interaction Enabled" checkbox for my UIView extended class in the Inspector.
I've also tried with :
[[self nextResponder] touchesBegan:touches withevent:event];
or
[[self.superview nextResponder] touchesBegan:touches withevent:event];
or
[super touchesBegan:touches withEvent:event];
or
[super touchesBegan:touches withEvent:event];
in the touchEvents methods of my extended UIView class.
I haven't succeeded in getting touch events to the UIScrollView.
On the other hand, if I add scrollView and my extended UIView class programatically, maintaining the same hierarchy, UIScrolView get touch events, but not its subview (my UIView extended class).
I don't want both UIScrollView and my UIView extended class to manage touch events at the same time. I just want to toggle between UIScrollView and my UIView extended class for listening to touch evebnts, by using a button for example.
Is it possible, in this case I am explaining, to select which, UIScrollView or my UIView extended class, should listen to touch events?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIScrollView 有一个
scrollEnabled
属性。如果将此设置为YES
,则滚动视图将可滚动。如果没有,事件将沿着响应者链向上转发。所以尝试改变这个属性。阅读 UIScrollView 的 文档!
至于在 nib 中设置与在 XCode 中设置之间的区别,您的代码设置和 nib 版本在某些重要方面并不完全匹配。我看不到您的笔尖或程序设置,因此无法为您提供更多帮助。
A UIScrollView has a
scrollEnabled
property. If you set this toYES
, the scroll view will be scrollable. If not, the events will be forwarded up the responder chain. So try changing that property.Read the docs for UIScrollView!
As for the difference between setting up in a nib versus XCode, your code setup and your nib version aren't quite matching some important way. I can't see your nib or your programmatic setup so can't help you much more with that.
您需要为滚动视图启用用户交互:
将gestureRecognizer添加到子视图:
并检测点击的视图:
You need to enable user interaction for scroll view:
Add gestureRecognizer to subview:
And detect the tapped view: