是否可以通过使用UIPanGestureRecognizer移动位于另一个ViewController(B)中的ViewController(A)? (以编程方式)

时间:2019-06-28 15:23:06

标签: ios swift uigesturerecognizer swift5

我正在尝试创建一个将通过手势移动的迷你侧菜单。

我已经在菜单ViewController中注册了手势,但是当我使用“平移”以移动BaseMenuController的左锚点时,它不会移动...

class MainController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    setupConstraints()
 }

 var baseMenuViewLeftAnchor: NSLayoutConstraint!
 func setupConstraints() {
    let baseMenuController = BaseMenuController()
    let baseMenuView = baseMenuController.view!
    baseMenuView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(baseMenuView)
    addChild(baseMenuController)

    baseMenuViewLeftAnchor = baseMenuView.leftAnchor.constraint(equalTo: view.leftAnchor)
    menuViewLeftAnchor.isActive = true
    baseMenuView.heightAnchor.constraint(equalToConstant: 200).isActive = true
    baseMenuView.widthAnchor.constraint(equalToConstant: 200).isActive = true
    baseMenuView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true

}



}

class BaseMenuController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = .white
    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
    view.addGestureRecognizer(panGesture)
}

@objc func handlePan(gesture: UIPanGestureRecognizer) {
    let mainController = MainController()
    let mainView = mainController.view!
    let translation = gesture.translation(in: mainView)
    let x = translation.x
    mainController.baseMenuViewLeftAnchor.constant = x
}
}

0 个答案:

没有答案