在视图IOS中禁用用户交互

New*_*bee 27 iphone xcode cocoa-touch objective-c ios

我正在使用以下代码禁用和启用视图....

[self.view setUserInteractionEnabled:NO];
[self.view setUserInteractionEnabled:YES];
Run Code Online (Sandbox Code Playgroud)

如果我这样做,所有子视图也会受到影响...所有都被禁用,我该怎么做只针对特定视图?可能吗?

Luk*_*uke 33

它完全相同,假设您的其他视图是成员或您可以遍历self.view子视图数组,如下所示:

MyViewController.h

UIView* otherView;
Run Code Online (Sandbox Code Playgroud)

MyViewController.m

otherView.userInteractionEnabled = NO; // or YES, as you desire.
Run Code Online (Sandbox Code Playgroud)

要么:

for (int i = 0; i < [[self.view subviews] count]; i++)
{
    UIView* view = [[self.view subviews] objectAtIndex: i];

    // now either check the tag property of view or however else you know
    // it's the one you want, and then change the userInteractionEnabled property.
}
Run Code Online (Sandbox Code Playgroud)


Rah*_*ina 13

在快速UIView确实有财产userInteractionEnabled,使其响应或不.要使完整View无响应使用代码:

// make screen unresponsive
self.view.userInteractionEnabled = false
//make navigation bar unresponsive
self.navigationController!.view.userInteractionEnabled = false

// make screen responsive
self.view.userInteractionEnabled = true
//make navigation bar responsive
self.navigationController!.view.userInteractionEnabled = true
Run Code Online (Sandbox Code Playgroud)


Ans*_*arg 5

for (UIView* view in self.view.subviews) {

    if ([view isKindOfClass:[/*"which ever class u want eg UITextField "*/ class]])

        [view setUserInteractionEnabled:NO];

}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.快乐的编码:)