Swift 項目中使用抽屜效果基於MMDrawerController
// AppDelegate.swift
var drawer = MMDrawerController()
let leftViewController = LeftViewController()
let rightViewController = RightViewController()
let centerViewController = ViewController()
let centerNav = UINavigationController.init(rootViewController: centerViewController)
// 設置左右控制器
drawer = MMDrawerController.init(centerViewController: centerNav, leftDrawerViewController: leftViewController, rightDrawerViewController: rightViewController)
// 側拉距離
drawer.maximumLeftDrawerWidth = 0.7 * width
drawer.maximumRightDrawerWidth = 0.7 * width
// 手勢
drawer.openDrawerGestureModeMask = MMOpenDrawerGestureMode.All
drawer.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.All
//設置動畫,這裡是設置打開側欄透明度從0到1
drawer.setDrawerVisualStateBlock { (drawerController, drawerSide, percentVisible) -> Void in
var sideDrawerViewController:UIViewController?
if(drawerSide == MMDrawerSide.Left){
sideDrawerViewController = drawerController.leftDrawerViewController;
}
if(drawerSide == MMDrawerSide.Left){
sideDrawerViewController = drawerController.rightDrawerViewController;
}
sideDrawerViewController?.view.alpha = percentVisible
}
self.window?.rootViewController = drawer
在左側VC中的tableview 的點擊方法中
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch (indexPath.row){
case 0:
let centerViewController = ViewController()
let centerNavigationController = UINavigationController(rootViewController: centerViewController)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.drawer.centerViewController = centerNavigationController
appDelegate.drawer.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
default:
let otherViewController = TestViewController()
let otherNavigationController = UINavigationController(rootViewController: otherViewController)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.drawer.centerViewController = otherNavigationController
appDelegate.drawer.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
}
}