18

我正在使用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;
    }
}

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

4

9 回答 9

31

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

override func viewDidLoad() {
    super.viewDidLoad()

    if self.revealViewController() != nil {

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

}

代码适用于 TAP 和 PAN 手势。

于 2015-03-25T18:41:24.383 回答
23

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

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

[myView addGestureRecognizer:tap];

当点击前视图时,这应该会导致后视图关闭,这是SWRevealViewController默认行为。

于 2014-03-16T20:40:37.960 回答
12

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

这是@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
            }
        )
    }
}
于 2015-08-11T12:16:00.303 回答
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;
        }];
    }
}

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

于 2014-04-24T10:45:46.813 回答
4

考虑这个简单的解决方案,效果很好

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

简单用法UIViewController

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

    setupMenuGestureRecognizer()
}
于 2016-07-01T07:35:55.760 回答
3

将以下代码放在菜单视图控制器上,它对我有用

@interface SidebarTableViewController() {
    UIView* coverView;
}

- (void)viewWillDisappear:(BOOL)animated {
    [coverView removeFromSuperview];
    //[self.revealViewController.frontViewController.view setUserInteractionEnabled:YES];
    // get your window screen size
}

- (void)viewWillAppear:(BOOL)animated {
    // get your window screen size
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    //create a new view with the same size
    coverView = [[UIView alloc] initWithFrame:screenRect];
    // change the background color to black and the opacity to 0.6
    coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    // add this new view to your main view
    [self.revealViewController.frontViewController.view addSubview:coverView];
    // [self.revealViewController.frontViewController.view setUserInteractionEnabled:NO];
}
于 2015-11-14T13:01:55.543 回答
2

感谢mixth & avdyushin的大力帮助。这是从那里获得答案的 Swift 4 版本,可以节省您的几个小时:

extension UIViewController: SWRevealViewControllerDelegate {

func setupMenuGestureRecognizer() {

    revealViewController().delegate = self

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

//MARK: - SWRevealViewControllerDelegate
public func revealController(_ revealController: SWRevealViewController!, willMoveTo position: FrontViewPosition) {
    let tagId = 112151
    print("revealController delegate called")
    if revealController.frontViewPosition == FrontViewPosition.right {
        let lock = self.view.viewWithTag(tagId)
        UIView.animate(withDuration: 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.black
        lock.addGestureRecognizer(UITapGestureRecognizer(target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:))))
        self.view.addSubview(lock)
        UIView.animate(withDuration: 0.75, animations: {
            lock.alpha = 0.333
        }
        )
    }
}

}

现在从后视控制器的viewDidLoad调用此函数setupMenuGestureRecognizer 。

或者您也可以直接在您的后视控制器类中实现SWRevealViewControllerDelegate,并在类本身中使用 SWRevealViewControllerDelegate 函数。

于 2017-10-26T12:55:35.707 回答
0

我使用了tapGestureRecognizer,但仍然存在一些问题。我试过了,效果很好!

定义类:

@interface IgnoreView : UIView

@property (nonatomic, assign) BOOL shouldAllTouchesBeForMe;

@end

实施:

@implementation IgnoreView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if( self.shouldAllTouchesBeForMe ){
        return self;
    }
    return [super hitTest:point withEvent:event];
}

@end

然后在 IgnoreView 类的 Interface Builder 中创建您的 View 类

然后,在您的 ViewController 中,执行以下操作:

在-viewDidLoad

self.revealViewController.delegate = self;
[self.view addGestureRecognizer:self.revealViewController.tapGestureRecognizer];

您的视图控制器中的实现也:

- (void)revealController:(SWRevealViewController *)revealController didMoveToPosition:(FrontViewPosition)position
{
    IgnoreView *i = (id)self.view;
    i.shouldAllTouchesBeForMe = position == FrontViewPositionRight;
}

你都准备好了!

于 2015-09-08T19:09:18.553 回答
-2

在您的菜单视图控制器上,添加以下内容:

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

然后在您的Menu Item View Controller上,将其添加到viewDidLoad

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

还可以在这里查看我的答案。它解决了前视图交互(加上滑动手势)的问题。

于 2014-05-26T04:10:13.037 回答