SWRevealViewController:在显示后视图时删除前视图上的交互

时间:2014-03-24 05:04:30

标签: ios uiviewcontroller user-interaction swrevealviewcontroller

我需要在显示后视图时禁用前视图上的用户交互。发现其他人也在问同样的事情,但却无法真正了解我在哪里或如何实施我见过的代码。

例如:我从link找到了此代码,

- (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;
    } 
}

还找到了其他一些链接

我有这段代码,但不确定插入此代码的正确位置。我尝试在我的前/后视图中添加它,也在SWRevealViewController方法中添加但没有成功

感谢有人能指出我正确的方向。

15 个答案:

答案 0 :(得分:51)

我最近提出了一个我想分享的解决方案 (对不起,如果它已经晚了2个月)。

要在菜单打开时禁用前视图上的用户互动,我在 MenuViewController 上添加了以下代码:

viewWillAppear 上:

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

viewWillDisappear

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

这将禁用前视图控制器上的所有用户交互,这意味着关闭菜单的滑动/点按手势也将被禁用。

现在,我创建了一个 ParentViewController ,并使所有视图控制器(菜单项)成为它的子类。

在我的 viewDidLoad 上,我输入了以下代码:

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

如果您此时运行应用程序,则可能会出现Tap Gesture工作(点击前视图将关闭菜单),但不是Pan Gesture。我不确定为什么会这样,但为了让幻灯片手势关闭您的菜单,请在 MenuViewController 中添加以下代码:

viewWillAppear 上:

[self.revealViewController.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

总结一下,这就是您需要的:

MenuViewController

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

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

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

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

在您的菜单项上'视图控制器(你可以为所有人创建一个 ParentViewController ):

-(void)viewDidLoad {
    [super viewDidLoad];

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

希望这有帮助!

答案 1 :(得分:11)

我使用了另一种方法来实现相同的结果,不确定它是否有帮助。

SWRevealViewControllerDelegate 分配给 AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    SWRevealViewController* reveal = (SWRevealViewController*)self.window.rootViewController;
    reveal.delegate = self;

    // other bootstrapping code
}

然后在委托方法-(void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position中,如下所示:

-(void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position
{
    if(position == FrontViewPositionLeft){
        [revealController.frontViewController.view setUserInteractionEnabled:YES];
        [revealController.frontViewController.revealViewController tapGestureRecognizer];
    }else{
        [revealController.frontViewController.view setUserInteractionEnabled:NO];
    }
}

更新:添加此行[revealController.frontViewController.revealViewController tapGestureRecognizer]以在点击frontviewcontroller时关闭显示的控制器

答案 2 :(得分:4)

Swift版本给@hardluckbaby回答:

MenuViewController 后视图控制器)中:

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

    self.revealViewController().frontViewController.view.userInteractionEnabled = false
    self.revealViewController().view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}

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

    self.revealViewController().frontViewController.view.userInteractionEnabled = true
}

FrontViewController (您可以为所有前视图控制器创建 ParentViewController ,如@hardluckbaby所说):

override func viewDidLoad() {
    super.viewDidLoad()

    if let revealController = self.revealViewController() {
        revealController.panGestureRecognizer()
        revealController.tapGestureRecognizer()
    }
}

答案 3 :(得分:3)

约翰解释的嘿伙计们: 虽然这些解决方案确实有效,但我认为它们中的任何一个都不能解决原始问题,这实际上非常简单:

涉及两个步骤:

1)将以下方法添加到FrontViewController.m:

- (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;
    } 
}

2)使前视图控制器成为FrontViewController.h文件中SWRevealViewController的委托:

(例如@interface HomeViewController:UIViewController) 我的FrontViewController被命名为HomeViewController

以及FrontViewController.m文件,以及ViewDidLoad上的以下内容:

self.revealViewController.delegate = self; 问题解决了!比创建父类等容易得多。

这将帮助您解决FrontView控制器的用户交互问题,我只想添加以上Xun的响应中的以下更改,当用户点击FrontViewController时,您将解决用户交互和隐藏菜单。

- (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;
      //Hides the menu when user taps FrontViewController
       [revealController.frontViewController.revealViewController tapGestureRecognizer];
    } 
}

答案 4 :(得分:2)

  

Swift 3.0 简单快捷的方法。

这里的Frontviewcontoller代码......

override func viewDidLoad() {
    super.viewDidLoad()
     if self.revealViewController() != nil {
        let rewel:SWRevealViewController  = revealViewController()
        rewel.panGestureRecognizer()
        rewel.tapGestureRecognizer() 
    }
}

这里的SideDrowerviewcontoller代码......

override func viewWillAppear(_ animated: Bool) {
  let rewel = self.revealViewController()
      rewel?.frontViewController.view.isUserInteractionEnabled = false
    rewel?.frontViewController.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}

override func viewWillDisappear(_ animated: Bool) {
    let rewel = self.revealViewController()
        rewel?.frontViewController.view.isUserInteractionEnabled = true
}

答案 5 :(得分:1)

打开后视图时,在前视图中添加子视图。

答案 6 :(得分:1)

在菜单项控制器的viewWillAppear方法中,只需在前视图上创建一个覆盖按钮,并将动作设置为revealToggle:revealViewController

-(void)viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];
    overlayView = [UIButton buttonWithType:UIButtonTypeCustom];
    overlayView.frame = self.revealViewController.frontViewController.view.bounds;
    overlayView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.8];
    overlayView.tag = 999;
    [overlayView addTarget:self.revealViewController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
    [overlayView addTarget:self.revealViewController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchDragOutside];
    [self.revealViewController.frontViewController.view addSubview:overlayView];
}

在revealTogglle方法中删除叠加按钮(如果有):

- (void)revealToggleAnimated:(BOOL)animated
{
    UIButton *overlayView = (UIButton*)[self.view viewWithTag:999];
    if (overlayView) {
        [overlayView removeFromSuperview];
        overlayView = nil;
    }
    // rest of the code...
}

答案 7 :(得分:1)

虽然这些解决方案确实有效,但我认为它们中的任何一个都没有解决原始问题,实际上这很简单:

涉及两个步骤:

1)将以下方法添加到 FrontViewController.m

- (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;
    } 
}

2)使前视图控制器成为 FrontViewController.h 文件中SWRevealViewController的委托:

(e.g. @interface HomeViewController : UIViewController <SWRevealViewControllerDelegate>)

我的FrontViewController被命名为HomeViewController

以及 FrontViewController.m 文件,以及ViewDidLoad上的以下内容:

self.revealViewController.delegate = self;

问题解决了!比创建父类等容易得多。

答案 8 :(得分:1)

另一种方法是在显示后视图时显示叠加视图。您可以使用此更新的库https://github.com/NSRover/SWRevealViewController,并确保在显示后视图时包含shouldUseFrontViewOverlay = true。

答案 9 :(得分:1)

class SideMenuViewController: UITableViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    self.revealViewController().delegate = self
  }

}

extension SideMenuViewController: SWRevealViewControllerDelegate {

  func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
    if position == .Left {
      revealController.frontViewController.view.userInteractionEnabled = true
    }

    if position == .Right {
      revealController.frontViewController.view.userInteractionEnabled = false
    }
  }

}

答案 10 :(得分:0)

On MenuTableViewController/ Rear VC, add SWRevealViewControllerDelegate.

    override func viewDidLoad() {
        super.viewDidLoad()
        self.revealViewController().delegate = self

        if self.revealViewController() != nil {
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
    }

Add this delegate method.

func revealController(revealController: SWRevealViewController!, didMoveToPosition position: FrontViewPosition) {
        if(position.rawValue == 4)
        {
            //move to rear
            self.revealViewController().frontViewController.view.userInteractionEnabled =  false
        }
        else if (position.rawValue == 3)
        {
            //move to front - dashboard VC
            self.revealViewController().frontViewController.view.userInteractionEnabled =  true
        }
    }
    func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {

//will perform the same function as the above delegate method.
    }

答案 11 :(得分:0)

考虑以下解决方案,完美无缺

private let DimmingViewTag = 10001

extension UIViewController: SWRevealViewControllerDelegate {    

    func removeInteractionFromFrontViewController() {

        revealViewController().delegate = self

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

    //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()
        }
    }
}

UIViewController中的简单用法:

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

    removeInteractionFromFrontViewController()
}

答案 12 :(得分:0)

除了 hardluckbaby 回答。

  

如果此时运行您的应用,则会显示Tap   手势正常(点击前视图将关闭菜单),但不是   泛手势。我不确定为什么会这样,但是为了启用它   滑动手势关闭您的菜单,在您的菜单中添加以下代码   MenuViewController:

     

on viewWillAppear:

[self.revealViewController.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

它增加了一些不受欢迎的行为,例如:平移手势会在启动时关闭后视图。

默认平移手势可能无效,如果您将其添加到您自己的视图某处,例如前视图控制器的 viewDidLoad

[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

删除这些行,这应该按预期工作,用于平移和点按手势

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

答案 13 :(得分:0)

我正在使用viewWillAppear和viewWillDisappear函数,但因为我有侧视图中几乎每个项目的子视图。我的问题是我有一个输入字段激活(键盘显示)并访问侧面菜单。在根菜单中键盘隐藏但在我进入子菜单键盘后再次显示。为了解决这个问题,我改变了方法来启用和禁用revealController中的交互,如下所示:

- (void)revealController:(SWRevealViewController *)revealController 
        didMoveToPosition:(FrontViewPosition)position {
    if (position == FrontViewPositionRight) {
        [self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];
    } else if (position == FrontViewPositionLeft) {
        [self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];
    }
}

答案 14 :(得分:0)

首先设置你的代表:  self.revealViewController.delegate = self; 代表方法如下:

- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position
{
    static NSInteger tagLockView = 123456789;
    if (revealController.frontViewPosition == FrontViewPositionRight)
    {
        UIView *lockView = [self.view viewWithTag:tagLockView];

        [UIView animateWithDuration:0.3 animations:^{
            lockView.alpha = 0;
        } completion:^(BOOL finished) {
            [lockView removeFromSuperview];
        }];
    }
    else if (revealController.frontViewPosition == FrontViewPositionLeft)
    {
        UIView *lockView = [[UIView alloc] initWithFrame:self.view.bounds];
        lockView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        lockView.tag = tagLockView;
        lockView.backgroundColor = [UIColor blackColor];
        lockView.alpha = 0;
        [lockView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self.revealViewController action:@selector(revealToggle:)]];
        [self.view addSubview:lockView];
        [UIView animateWithDuration:0.3 animations:^{
            lockView.alpha = 0.5;
        }];
    }

}