如何在RearViewController的视图处于活动状态时禁用前ViewController的视图Swift 3.0中的SWRevealViewController

时间:2017-03-30 22:10:03

标签: swift3 swrevealviewcontroller

如何禁用用户与前视图控制器的交互 - 即在我的情况下使用类ViewController,当显示后部菜单时。使用当前代码,当显示后部菜单时,bookCleaningButton未被禁用。

import UIKit
class ViewController: UIViewController, SWRevealViewControllerDelegate {

  @IBOutlet weak var menuButton: UIBarButtonItem!
  @IBOutlet weak var bookCleaningButton: UIButton!


override func viewDidLoad() {
    super.viewDidLoad()
      self.revealViewController().rearViewRevealWidth = self.view.frame.width - 80

 //revela the menu if it is not nil
    if self.revealViewController() != nil {
        self.revealViewController().delegate = self
         menuButton.target = self.revealViewController()
           menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
         self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()   
    }
  }


func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
    if revealController.frontViewPosition == FrontViewPosition.left {
        self.view.isUserInteractionEnabled = false
    }
    else {
        self.view.isUserInteractionEnabled = true
     }
  }  
}

enter image description here

2 个答案:

答案 0 :(得分:4)

您应该在菜单视图控制器中处理此问题。显示视图控制器可以访问frontViewController,该属性可用于将userInteractionEnabled设置为false

因此,在您的菜单视图控制器中,使用viewWillAppear方法将此代码写入:

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

在同一菜单视图中,Controller在viewWillDisappear方法中添加以下代码:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.revealViewController().frontViewController.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    self.revealViewController().frontViewController.view.isUserInteractionEnabled = true
}

上面的代码还添加了其他手势,但这些手势可以是可选的。主要行动发生在以下两行:

self.revealViewController().frontViewController.view.isUserInteractionEnabled = false

self.revealViewController().frontViewController.view.isUserInteractionEnabled = true

希望这能解决您的问题。干杯。

答案 1 :(得分:0)

在前视图控制器类中。在viewdidload()方法上编写此代码。

override func viewDidLoad() {
        super.viewDidLoad()

        menuBtn.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)

        revealViewController().delegate = self

        // Once time - See documented SWRevealViewController.h
        revealViewController().tapGestureRecognizer()
        revealViewController().panGestureRecognizer()
}

然后从frontviewcontroller中的SWRevealviewcontroller使用此委托

func revealController(_ revealController: SWRevealViewController!, willMoveTo position: FrontViewPosition) {
        if position == FrontViewPosition.right {
            revealController.frontViewController.view.isUserInteractionEnabled = false
        }
        else {
            revealController.frontViewController.view.isUserInteractionEnabled = true
        }
    }

这将符合您的期望......