简体   繁体   中英

Disable user interaction but allow tap gesture in UIViewController

I would like to disable the user interaction on the entire view but also allow the tap gesture on the view. Let say there is a menu above the main view and if the user taps on the main view again the rear menu disappears. However all other interaction with the main view should be disabled as long as the menu is shown. Here is how I try to do that: I defined the tapGestureRecognizer like this:

@interface GroupMasterViewController () <SWRevealViewControllerDelegate>

@property NSMutableArray *groups;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;

@end

In my ViewDidLoad method I have this:

self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self.revealViewController action:@selector(rightRevealToggle:)];
[self.view addGestureRecognizer:self.tapGestureRecognizer];
self.tapGestureRecognizer.enabled = NO;

And in the delegate method which gets fired when the menu opens or closes:

- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position
{
    if (position == FrontViewPositionLeftSide) {
        self.tapGestureRecognizer.enabled = YES;
        self.view.userInteractionEnabled = NO;
        self.tabBarController.tabBar.userInteractionEnabled = NO;
    }
    else if (position == FrontViewPositionLeft){
        self.tapGestureRecognizer.enabled = NO;
        self.view.userInteractionEnabled = YES;
        self.tabBarController.tabBar.userInteractionEnabled = YES;

    }
}

The Problem is that the tapGestureRecognizer does not work anymore if I disable the user interaction.

Thank you in advance for any help.

It would be conflict if you try to enable tapGestureRecogizer while disabling user interaction, so this is the most straight-forward solution in my honest opinion: Instead of turning off user interaction of the whole view, you should do that to each of the views that you want to not response to touches. For example:

- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position
{
    if (position == FrontViewPositionLeftSide) {
        self.tapGestureRecognizer.enabled = YES;
        self.button1.userInteractionEnabled = NO;
        self.button2.userInteractionEnabled = NO;
        self.tabBarController.tabBar.userInteractionEnabled = NO;
    }
    else if (position == FrontViewPositionLeft){
        self.tapGestureRecognizer.enabled = NO;
        self.button1.userInteractionEnabled = YES;
        self.button2.userInteractionEnabled = YES;
        self.tabBarController.tabBar.userInteractionEnabled = YES;

    }
}

I think it is not possible to disable user interaction and then expect some user interaction events. You may show progress bar for a particular event to prevent user interaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM