SWRevealViewController close rear view when tappin

2019-01-17 16:41发布

I am using SWRevealViewController in order to implement a side nav menu in my app. I would like to make it so that the front view cannot be interacted with when the rear view is opened, except that when the user taps the front view, the rear view will close and the front view can be interacted with again. I have these two SWRevealViewController delegate methods that currently remove interaction from the front view.

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

- (void)revealController:(SWRevealViewController *)revealController didMoveToPosition:    (FrontViewPosition)position {
    if(position == FrontViewPositionLeft) {
        self.view.userInteractionEnabled = YES;
    } else {
        self.view.userInteractionEnabled = NO;
    }
}

However this doesn't cause the rear view to close when the front view is tapped. Any help would be greatly appreciated, thanks!

9条回答
爷的心禁止访问
2楼-- · 2019-01-17 17:15

In ViewDidLoad of your frontViewController you need to add a UITapGestureRecognizer

SWRevealViewController *revealController = [self revealViewController];
UITapGestureRecognizer *tap = [revealController tapGestureRecognizer];
tap.delegate = self;

[myView addGestureRecognizer:tap];

This should cause the rear view to close when the front view is tapped which is SWRevealViewController's default behaviour.

查看更多
贼婆χ
3楼-- · 2019-01-17 17:16

Consider this simple solution, works perfect

private let DimmingViewTag = 10001

extension UIViewController: SWRevealViewControllerDelegate {    

    func setupMenuGestureRecognizer() {

        revealViewController().delegate = self

        view.addGestureRecognizer(revealViewController().panGestureRecognizer())
        view.addGestureRecognizer(revealViewController().tapGestureRecognizer())
    }

    //MARK: - SWRevealViewControllerDelegate

    public func revealController(revealController: SWRevealViewController!, didMoveToPosition position: FrontViewPosition) {

        if case .Right = position {

            let dimmingView = UIView(frame: view.frame)
            dimmingView.tag = DimmingViewTag

            view.addSubview(dimmingView)
            view.bringSubviewToFront(dimmingView)

        } else {
            view.viewWithTag(DimmingViewTag)?.removeFromSuperview()
        }
    }
}

Simple usage in UIViewController:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    setupMenuGestureRecognizer()
}
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-17 17:16

On your Menu view controller, add this:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    [self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];
}

Then on your Menu Item View Controller, add this on viewDidLoad:

SWRevealViewController *revealController = [self revealViewController];
[revealController tapGestureRecognizer];

Also check out my answer here. It addresses the problem on front view interaction (plus a slide gesture).

查看更多
登录 后发表回答