UIScrollview 和子视图触摸事件管理

发布于 2024-10-21 01:21:33 字数 970 浏览 4 评论 0原文

我有主要的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春如此纠结 2024-10-28 01:21:34

UIScrollView 有一个 scrollEnabled 属性。如果将此设置为YES,则滚动视图将可滚动。如果没有,事件将沿着响应者链向上转发。所以尝试改变这个属性。

阅读 UIScrollView 的 文档

至于在 nib 中设置与在 XCode 中设置之间的区别,您的代码设置和 nib 版本在某些重要方面并不完全匹配。我看不到您的笔尖或程序设置,因此无法为您提供更多帮助。

A UIScrollView has a scrollEnabled property. If you set this to YES, 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.

离笑几人歌 2024-10-28 01:21:34

您需要为滚动视图启用用户交互:

[self.scrollView setUserInteractionEnabled:YES];

将gestureRecognizer添加到子视图:

for (int i = 0; i < pos.count; i++) {
        SCVerticalBarsChartBarView *bar = [[SCVerticalBarsChartBarView alloc]initWithBlaBlaBla];
        [bar addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(someAction:)]];
        bar.tag = i;
        [self.scrollView addSubview:bar];            
    }

并检测点击的视图:

-(void)someAction:(UITapGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateEnded) {
    if ([recognizer.view isKindOfClass:[SCVerticalBarsChartBarView class]]) {
        SCVerticalBarsChartBarView *tmpButt=(SCVerticalBarsChartBarView *)recognizer.view;
        NSLog(@"%d", tmpButt.tag);
    }
  }
}

You need to enable user interaction for scroll view:

[self.scrollView setUserInteractionEnabled:YES];

Add gestureRecognizer to subview:

for (int i = 0; i < pos.count; i++) {
        SCVerticalBarsChartBarView *bar = [[SCVerticalBarsChartBarView alloc]initWithBlaBlaBla];
        [bar addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(someAction:)]];
        bar.tag = i;
        [self.scrollView addSubview:bar];            
    }

And detect the tapped view:

-(void)someAction:(UITapGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateEnded) {
    if ([recognizer.view isKindOfClass:[SCVerticalBarsChartBarView class]]) {
        SCVerticalBarsChartBarView *tmpButt=(SCVerticalBarsChartBarView *)recognizer.view;
        NSLog(@"%d", tmpButt.tag);
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文