側邊抽屜是許多應用採用的導覽模式。自己從手勢寫起成本不低,實務上多半會用成熟框架。本文以 MMDrawerController 為例,說明如何同時掛上左側欄與右側欄。

這個實作很靈活:可以左右兩側都開,也可以只開左側或只開右側。


#import <UIKit/UIKit.h> @interface YJFAppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#import "YJFAppDelegate.h"
#import "CustomizedNavigationController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@implementation YJFAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// 主視圖
FirstViewController *firstVC = [[FirstViewController alloc] init];
// 左側視圖
SecondViewController *secondVC = [[SecondViewController alloc] init];
// 右側視圖
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
CustomizedNavigationController *navigationVC = [[CustomizedNavigationController alloc] initWithRootViewController:firstVC];
CustomizedNavigationController *leftNavigationVC = [[CustomizedNavigationController alloc] initWithRootViewController:secondVC];
CustomizedNavigationController *rightNavigationVC = [[CustomizedNavigationController alloc] initWithRootViewController:thirdVC];
// 抽屜管理器:可同時實作左右兩側
MMDrawerController *rooVC = [[MMDrawerController alloc] initWithCenterViewController:navigationVC
leftDrawerViewController:leftNavigationVC
rightDrawerViewController:rightNavigationVC];
// 只實作左側欄
// MMDrawerController *rooVCLeft = [[MMDrawerController alloc] initWithCenterViewController:navigationVC
// leftDrawerViewController:firstVC];
// 只實作右側欄
// MMDrawerController *rooVCRight = [[MMDrawerController alloc] initWithCenterViewController:navigationVC
// rightDrawerViewController:thirdVC];
self.window.rootViewController = rooVC;
// 抽屜寬度
[rooVC setMaximumLeftDrawerWidth:270];
// 開關手勢範圍
[rooVC setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
[rooVC setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
// 左右側的呈現動畫(這裡都設為無動畫)
[[MMExampleDrawerVisualStateManager sharedManager] setRightDrawerAnimationType:MMDrawerAnimationTypeNone];
[[MMExampleDrawerVisualStateManager sharedManager] setLeftDrawerAnimationType:MMDrawerAnimationTypeNone];
// 若要自訂轉場效果,可掛上 visual state block
// [rooVC setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {
// MMDrawerControllerDrawerVisualStateBlock block =
// [[MMExampleDrawerVisualStateManager sharedManager] drawerVisualStateBlockForDrawerSide:drawerSide];
// if (block) { block(drawerController, drawerSide, percentVisible); }
// }];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
closeDrawerAnimated:completion: 關閉抽屜再切換畫面。