我在这里使用此代码块从右侧显示我的侧菜单,而不是从左侧显示
@objc func setupBarButtonItems(){
let mainWindow = UIApplication.shared.keyWindow
//initial position of side menu
menuController.view.frame = CGRect(x: maxX, y: 0, width: menuWidth, height: self.view.frame.height)
mainWindow?.addSubview(menuController.view)
addChildViewController(menuController)
//add side menu button to nav bar
let sideMenuButton = UIBarButtonItem(image: UIImage(named: "icons8-Menu-48")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(self.rightBarPressed))
let calendarButton = UIBarButtonItem(image: UIImage(named: "icons8-calendar-50")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(self.presentCalendar))
navigationItem.rightBarButtonItems = [sideMenuButton,calendarButton]
}
@objc func addPanGesture(){
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
self.view.addGestureRecognizer(panGesture)
}
@objc func handlePan(gesture: UIPanGestureRecognizer){
//will give you position of finger in swipe gesture
let translation = gesture.translation(in: view)
if gesture.state == .changed {
var x = translation.x
print(x)
x = min(-menuWidth,x)
x = max(0,-returnX)
let transform = CGAffineTransform(translationX: x , y: 0)
menuController.view.transform = transform
navigationController?.view.transform = transform
}else if gesture.state == .ended {
rightBarPressed()
}
//drag out the menu controller
}
但是,我的拖动有点缺陷。它可以准确地显示出来,但是当我尝试将其移回时,它会恢复到拉出状态。我知道向左滑动是向负方向滑动,向右滑动则相反。任何想法,我将如何更改最小和最大函数以使其正常工作
我添加了lil gif以显示当前性质