Superview 忽略 UserInteractionEnabled = NO?

发布于 2024-11-28 01:13:26 字数 124 浏览 4 评论 0原文

我有一个子视图,它以动画方式显示在屏幕上,当它在屏幕上时,我希望超级视图(在我的例子中,背景中的视图)忽略触摸事件。 我尝试了一切,但它不起作用。

有没有办法“强制”超级视图停止接收触摸事件?

谢谢!

I have a subview thats goes onto the screen animated, and when it is on the screen, I want the superview (in my case, the view in the background), to ignore touch events.
I tried everything but it does not work.

Is there any way to 'force' the superview to stop receiving touch events?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

待天淡蓝洁白时 2024-12-05 01:13:26

当您说超级视图时,我假设您指的是动画视图的超级视图。 Apple 的文档对于 userInteractionEnabled 的描述非常不具体,但我认为如果将其设置为 false,它将禁用特定视图上的触摸事件,但不会禁用其子视图上的触摸事件。我建议你递归地进行。下面是一个代码示例,您可以使用它来禁用/启用视图上的所有触摸事件:

- (void)setInteraction:(BOOL)allow onView:(UIView *)aView {
    [aView setUserInteractionEnabled:allow];
    for (UIView * v in [aView subviews]) {
        [self setInteraction:allow onView:v];
    }
}

然后您可以在超级视图 [self setInteraction:NO onView:[self superview]] 上调用此代码。当然,这也会禁用您的触摸事件,因为您在超级视图上递归地禁用它们。当然,您始终可以重新启用触摸事件[self setUserInteractionEnabled:NO]

另外,Apple 的 UIView 类参考提到一些 UI 组件会重写此方法:

注意:某些 UIKit 子类会覆盖此属性并返回不同的默认值。请参阅用于确定它是否为此属性返回不同值的任何类的文档。

When you say the superview, I assume you you mean the animating view's superview. Apple's documentation is very unspecific about userInteractionEnabled, but I think that if you set it to false, it disables touch events on the specific view, but not on its subviews. I would suggest that you do it recursively. Here is an example of code that you could use to disable/enable ALL touch events on a view:

- (void)setInteraction:(BOOL)allow onView:(UIView *)aView {
    [aView setUserInteractionEnabled:allow];
    for (UIView * v in [aView subviews]) {
        [self setInteraction:allow onView:v];
    }
}

You could then call this on your superview [self setInteraction:NO onView:[self superview]]. This would, of course, disable your touch events as well, since you are disabling them recursively on your superview. Of course you can always re-enable your touch events [self setUserInteractionEnabled:NO].

Also, Apple's UIView Class Reference mentions that some UI components override this method:

Note: Some UIKit subclasses override this property and return a different default value. See the documentation for any class you use to determine if it returns a different value for this property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文