当点击前视图时,SWRevealViewController关闭后视图

sha*_*her 18 objective-c ios swrevealviewcontroller

我正在使用SWRevealViewController,以便在我的应用程序中实现侧面导航菜单.我想这样做是为了在打开后视图时不能与前视图交互,除了当用户轻敲前视图时,后视图将关闭并且前视图可以再次交互.我有这两个SWRevealViewController委托方法,目前从前视图中删除交互.

- (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;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当轻敲前视图时,这不会导致后视图关闭.任何帮助将不胜感激,谢谢!

Ada*_*nic 30

如果您正在使用SWIFT,您可以在frontViewController中执行以下操作:

override func viewDidLoad() {
    super.viewDidLoad()

    if self.revealViewController() != nil {

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

}
Run Code Online (Sandbox Code Playgroud)

代码适用于TAP和PAN手势.

  • 任何人都知道如何测试侧面菜单是否在swift中打开? (2认同)

Nic*_*ick 23

在frontViewController的ViewDidLoad中,您需要添加一个 UITapGestureRecognizer

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

[myView addGestureRecognizer:tap];
Run Code Online (Sandbox Code Playgroud)

这会导致后视图在轻敲前视图时关闭,这是SWRevealViewController默认行为.


mix*_*xth 12

编辑:感谢Marroc评论,改变UIView的autoresizingMask以适应Swift 2

这是@ avdyushin的答案的Swift-SWRevealViewController 2.x版本:

func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
    let tagId = 4207868622

    if revealController.frontViewPosition == FrontViewPosition.Right {
        let lock = self.view.viewWithTag(tagId)
        UIView.animateWithDuration(0.25, animations: {
                lock?.alpha = 0
            }, completion: {(finished: Bool) in
                lock?.removeFromSuperview()
            }
        )
        lock?.removeFromSuperview()
    } else if revealController.frontViewPosition == FrontViewPosition.Left {
        let lock = UIView(frame: self.view.bounds)
        lock.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
        lock.tag = tagId
        lock.alpha = 0
        lock.backgroundColor = UIColor.blackColor()
        lock.addGestureRecognizer(UITapGestureRecognizer(target: self.revealViewController(), action: "revealToggle:"))
        self.view.addSubview(lock)
        UIView.animateWithDuration(0.75, animations: {
                lock.alpha = 0.333
            }
        )
    }
}
Run Code Online (Sandbox Code Playgroud)


avd*_*hin 6

  1. 子类SWRevealViewController.
  2. 实现revealController:willMoveToPosition:SWRevealViewControllerDelegate.
  3. 在前视图控制器上设置全屏视图以覆盖所有触摸.
  4. 添加点按手势识别器以隐藏菜单.

带有漂亮动画的示例:

- (void)revealController:(SWRevealViewController *)revealController willMoveToPosition:(FrontViewPosition)position;
{
    static NSInteger tagLockView = 4207868622;
    if (revealController.frontViewPosition == FrontViewPositionRight) {
        UIView *lock = [self.frontViewController.view viewWithTag:tagLockView];
        [UIView animateWithDuration:0.25 animations:^{
            lock.alpha = 0;
        } completion:^(BOOL finished) {
            [lock removeFromSuperview];
        }];
    } else if (revealController.frontViewPosition == FrontViewPositionLeft) {
        UIView *lock = [[UIView alloc] initWithFrame:self.frontViewController.view.bounds];
        lock.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        lock.tag = tagLockView;
        lock.backgroundColor = [UIColor blackColor];
        lock.alpha = 0;
        [lock addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(revealToggle:)]];
        [self.frontViewController.view addSubview:lock];
        [UIView animateWithDuration:0.75 animations:^{
            lock.alpha = 0.333;
        }];
    }
}
Run Code Online (Sandbox Code Playgroud)

此解决方案适用于旧版本1.x SWRevealViewController.