如何禁用除最顶层视图之外的所有视图的触摸输入? [英] How to disable touch input to all views except the top-most view?

查看:151
本文介绍了如何禁用除最顶层视图之外的所有视图的触摸输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个子视图的视图。当用户点按子视图时,子视图的大小会扩大以覆盖屏幕的大部分,但其他一些子视图仍在下面显示。

I have a view with multiple subviews. When a user taps a subview, the subview expands in size to cover most of the screen, but some of the other subviews are still visible underneath.

我希望我的应用当其中一个子视图像这样展开时,忽略其他子视图上的触摸。有没有一个简单的方法来实现这一点?我可以编写代码来处理这个,但我希望有一个更简单的内置方式。

I want my app to ignore touches on the other subviews when one of the subviews is "expanded" like this. Is there a simple way to achieve this? I can write code to handle this, but I was hoping there's a simpler built-in way.

推荐答案

[[yourSuperView subviews]
   makeObjectsPerformSelector:@selector(setUserInteractionEnabled:)
   withObject:[NSNumber numberWithBool:FALSE]];

这将禁用视图的立即子视图的userInteraction.Then userInteraction到你想要的唯一视图

which will disable userInteraction of a view's immediate subviews..Then give userInteraction to the only view you wanted

yourTouchableView.setUserInteraction = TRUE;



EDIT:



在父视图上禁用userInteraction不会禁用其子对象的userInteraction。所以上面的代码(我的意思是 c> makeObjectsPerformSelector:)只会禁用父项的直接子视图的userInteraction。

It seems in iOS disabling userInteraction on a parent view doesn't disable userInteraction on its childs.. So the code above (I mean the one with makeObjectsPerformSelector:)will only work to disable userInteraction of a parent's immediate subviews..

查看用户madewulf的回答,递归地获取所有子视图并禁用所有他们。或者如果你需要在项目的许多地方禁用此视图的userInteraction,你可以分类UIView添加该功能..这样的东西会做..

See user madewulf's answer which recursively get all subviews and disable user interaction of all of them. Or if you need to disable userInteraction of this view in many places in the project, You can categorize UIView to add that feature.. Something like this will do..

@interface UIView (UserInteractionFeatures)
-(void)setRecursiveUserInteraction:(BOOL)value;
@end

@implementation UIView(UserInteractionFeatures)
-(void)setRecursiveUserInteraction:(BOOL)value{
    self.userInteractionEnabled =   value;
    for (UIView *view in [self subviews]) {
        [view setRecursiveUserInteraction:value];
    }
}
@end

/ p>

Now you can call

[yourSuperView setRecursiveUserInteraction:NO];

此外,user @ lxt的建议是在所有视图之上添加一个不可见的视图是另一种方式it ..

Also user @lxt's suggestion of adding an invisible view on top of all view's is one other way of doing it..

这篇关于如何禁用除最顶层视图之外的所有视图的触摸输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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