iOS開發之抽屜效果實現

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   for   

  說道抽屜效果在iOS中比較有名的第三方類庫就是PPRevealSideViewController。一說到第三方類庫就自然而然的想到我們的CocoaPods,今天的部落格中用CocoaPods引入PPRevealSideViewController,然後在我們的工程中以代碼結合storyboard來做出抽屜效果。

  一.在工程中用CocoaPods引入第三方外掛程式PPRevealSideViewController.

    (1).在終端中搜尋PPRevealSideViewController的版本

  

    (2).在Podfile中添加相應的版本庫

 

    (3).之後儲存一下Podfile檔案,然後執行pod install即可

 

  二、為我們的工程添加pch檔案

    因為用的是XCode6, 上面預設是沒有pch檔案的,如果我們想使用pch檔案,需要手動添加,添加步驟如下

    1.在XCode6中是麼有pch檔案的,如

 

 

    2.建立pch檔案

 

    

 

    3.配置pch檔案

      (1)、找工程的Targets->Build Settings->Apple LLVM 6.0 - Language

 

      (2)在Prefix Header下面的Debug和Release下添加$(SRCROOT)/工程名/pch檔案,入

    

  三、使用PPRevealSideViewController來實現抽屜效果

    當然了首先在pch檔案中引入我們的第三方類庫,然後使用即可

    1.在storyboard拖出來我們要用的視圖控制器,點擊主介面上的按鈕會以抽屜的形式展示出導航頁,然後在導航頁導航到各個介面,之後在從各個頁面回到主介面

 

    2.在AppDelegate中初始化我們的PPRevealSideViewController並設定為啟動頁面代碼如下:

 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2     // Override point for customization after application launch. 3      4     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 5      6     //擷取主視圖的導航控制器 7     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; 8     UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"NavigationController"]; 9     10     //建立PPRevealSideViewController,並設定根視圖(首頁面的導航視圖)11     PPRevealSideViewController *sideViewController = [[PPRevealSideViewController alloc] initWithRootViewController:vc];12     13     sideViewController.fakeiOS7StatusBarColor = [UIColor whiteColor];14     15     //把sideViewController設定成根視圖控制器16     self.window.rootViewController = sideViewController;17     18     [self.window makeKeyAndVisible];19     20     return YES;21 }
View Code

 

    3.在主介面使用PPRevealSideViewController來推出導航頁

1 - (IBAction)tapItem:(id)sender {2     3     UIStoryboard *storybaord = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];4     UITableViewController *table = [storybaord instantiateViewControllerWithIdentifier:@"CustomViewViewController"];5     [self.revealSideViewController pushViewController:table onDirection:PPRevealSideDirectionLeft animated:YES];6 }
View Code

 

    4.在導航頁點擊不同的按鈕使用PPRevealSideViewController跳轉到不同的controller

 1 - (IBAction)tap1:(id)sender { 2      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; 3     UIViewController *one = [storyboard instantiateViewControllerWithIdentifier:@"one"]; 4     [self.revealSideViewController popViewControllerWithNewCenterController:one animated:YES]; 5 } 6  7 - (IBAction)tap2:(id)sender { 8      9     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];10     UIViewController *one = [storyboard instantiateViewControllerWithIdentifier:@"two"];11     [self.revealSideViewController popViewControllerWithNewCenterController:one animated:YES];12     13 }14 15 - (IBAction)tap3:(id)sender {16     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];17     UIViewController *one = [storyboard instantiateViewControllerWithIdentifier:@"three"];18     [self.revealSideViewController popViewControllerWithNewCenterController:one animated:YES];19 }20 21 - (IBAction)tap4:(id)sender {22     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];23     UIViewController *one = [storyboard instantiateViewControllerWithIdentifier:@"four"];24     [self.revealSideViewController popViewControllerWithNewCenterController:one animated:YES];25 }
View Code

 

    5.各個頁面返回到主介面的代碼如下:

1 - (IBAction)tapPage:(id)sender {2     UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];3     4     UIViewController *view = [storyboard instantiateViewControllerWithIdentifier:@"NavigationController"];5     6     [self.revealSideViewController popViewControllerWithNewCenterController:view animated:YES];7 }
View Code

 

  四.到此效果實現完畢,下面是:

 

 

iOS開發之抽屜效果實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.