SWRevealViewController在超出“snappoint”之后关闭。

时间:2017-04-04 15:48:07

标签: objective-c swift uigesturerecognizer swrevealviewcontroller

我试图在我的应用程序中使SWRevealViewController工作。

我所做的是我不想从屏幕上的任何位置打开菜单,所以我用UIPanGestureRecognizer更改了所有UIScreenEdgePanGestureRecognizer,所以它只会从屏幕的一侧。

为了达到这个目的,我还改变了panGestureRecognizer方法。现在看来如下

- (UIScreenEdgePanGestureRecognizer*)panGestureRecognizer
{
    if ( _panGestureRecognizer == nil )
    {
        _panGestureRecognizer = [[SWRevealViewControllerPanGestureRecognizer alloc] initWithTarget:self action:@selector(_handleRevealGesture:)];
        _panGestureRecognizer.delegate = self;
        _panGestureRecognizer.edges = UIRectEdgeLeft;
        [_contentView.frontView addGestureRecognizer:_panGestureRecognizer];
    }
    return _panGestureRecognizer;
}

然而(不确定这个改变是否导致我的问题)当我从左侧打开菜单并将其展开时,它会在打开时正常捕捉到的位置将折叠回来。

所以让我们说它打开到500px的屏幕。当我将它拖到500px以上时,它会自动关闭菜单。

目前无法通过滑动再次关闭菜单。我现在所做的是添加一个手势识别器,它将触发revealViewController.revealToggle(animated: true)方法。

有没有人碰巧知道如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

导入#import "SWRevealViewController.h"

这两个手势可以帮助您打开和关闭sw菜单。拖动并单击以打开和关闭SW

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

正常打开sw菜单

[self.revealViewController revealToggleAnimated:YES]

答案 1 :(得分:0)

我最后通过使用SWRevealViewController附带的委托方法修复它,我的整个代码看起来像这样

import UIKit
import SWRevealViewController
import UIKit.UIGestureRecognizerSubclass

class ViewController: UIViewController, SWRevealViewControllerDelegate {

    @IBOutlet var openMenu: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        openMenu.target = self.revealViewController()
        openMenu.action = #selector(SWRevealViewController.revealToggle(_:))

        revealViewController().delegate = self
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

        self.hideMenuWhenTappedAround()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func revealControllerPanGestureShouldBegin(_ revealController: SWRevealViewController!) -> Bool {
        let point = revealController.panGestureRecognizer().location(in: self.view)

        if revealController.frontViewPosition == FrontViewPosition.left && point.x < 50.0 {
            return true
        }
        else if revealController.frontViewPosition == FrontViewPosition.right {
            return true
        }

        return false
    }

    func revealController(_ revealController: SWRevealViewController!, panGestureMovedToLocation location: CGFloat, progress: CGFloat) {
        if location >= revealController.rearViewRevealWidth {
            revealController.panGestureRecognizer().state = UIGestureRecognizerState.ended
        }
    }
}

self.hideMenuWhentappedAround()确保在您点按uiview上的任意位置时菜单将关闭。此方法是在UIViewController的扩展名中创建的,如下所示。

import UIKit
import SWRevealViewController

extension UIViewController {
    func hideMenuWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(SWRevealViewController.dismissMenu))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }

    func dismissMenu() {
        self.revealViewController().revealToggle(animated: true)
    }
}