iOS開發UI篇--一個側滑菜單SlidingMenu


一、簡介

側滑菜單已經成為app一個極常用的設計,不管是事務類,效率類還是生活類app。側滑菜單因Path 2.0和Facebook為開發者熟知,國內目前也有很多流行app用到了側滑菜單,比如QQ、網易郵箱、知乎等等。
iOS官方並沒有提供類似於側滑欄之類的組件,所以我們需要自己寫一個側滑欄控件,為了不要重復造輪子,我在github上找到了一個使用簡單方便,新手容易入手的側滑菜單控件,地址:https://github.com/John-Lluch/SWRevealViewController/tree/master/

下面我們就是使用上面的控件,來做一個側滑欄的小Demo,來教大家快速入門側滑欄控件。
Demo界面演示如下:

演示圖片 

二、使用說明

第一步:導入SWRevealViewController.h和SWRevealViewController.m文件

第二步:編寫中間顯示界面CenterViewController

在viewDidLoad方法中設置SWRevealViewController中的panGestureRecognizer方法,即可實現在主界面上滑動就可以出現左側或者右側菜單。設置revealToggle:方法就可以實現點擊進行左邊菜單和中間界面的切換。設置rightRevealToggle:方法就可以實現右邊菜單和中間界面的切換。下面就是中間界面的相關代碼:

   //注冊該頁面可以執行滑動切換 SWRevealViewController *revealController = self.revealViewController; [self.view addGestureRecognizer:revealController.panGestureRecognizer]; // 注冊該頁面可以執行點擊切換 [leftBtn addTarget:revealController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside]; [rightBtn addTarget:revealController action:@selector(rightRevealToggle:) forControlEvents:UIControlEventTouchUpInside];

 

第三步、編寫左側菜單欄LeftViewController

左側菜單欄是由一個UITableView組成的,我們在每個cell的點擊方法中執行 [revealViewController pushFrontViewController:viewController animated:YES];切換中間界面的操作。代碼如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ SWRevealViewController *revealViewController = self.revealViewController; UIViewController *viewController; switch (indexPath.row) { case 0: viewController = [[CenterView1Controller alloc] init]; break; case 1: viewController = [[CenterView2Controller alloc] init]; break; default: break; } [revealViewController pushFrontViewController:viewController animated:YES]; }

 

第四步、編寫右側菜單欄RightViewController

這里主要演示左側菜單欄,這里就不做過多描述。就以一個簡單的ViewController代替。

第五步、在AppDelegate.m文件中的- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions方法中配置以上界面,可以分別設置左側菜單欄、右側菜單欄和中間首頁。

詳見代碼注釋:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //左側菜單欄 LeftViewController *leftViewController = [[LeftViewController alloc] init]; //首頁 CenterView1Controller *centerView1Controller = [[CenterView1Controller alloc] init]; //右側菜單欄 RightViewController *rightViewController = [[RightViewController alloc] init]; SWRevealViewController *revealViewController = [[SWRevealViewController alloc] initWithRearViewController:leftViewController frontViewController:centerView1Controller]; revealViewController.rightViewController = rightViewController; //浮動層離左邊距的寬度 revealViewController.rearViewRevealWidth = 230; // revealViewController.rightViewRevealWidth = 230; //是否讓浮動層彈回原位 //mainRevealController.bounceBackOnOverdraw = NO; [revealViewController setFrontViewPosition:FrontViewPositionLeft animated:YES]; self.window.rootViewController = revealViewController; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; return YES; }

 

三、總結

接下來准備使用這個界面作為主框架,寫一系列關於IOS動畫的總結 和 facebook開源動畫項目pop動畫的使用的博客。敬請期待。

四、下載地址

github下載地址:https://github.com/yixiangboy/IOSAnimationDemo

如果覺得對你還有些用,給一顆star吧。你的支持是我繼續的動力。


博主的話

以前看過很多別人的博客,學到不少東西。現在准備自己也開始寫寫博客,希望能夠幫到一些人。 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM