您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS 抽屜效果實作:用 CocoaPods 集成 PPRevealSideViewController

編輯:iOS 相關教學

說到 iOS 的抽屜效果,比較有名的第三方類庫是 PPRevealSideViewController。而說到第三方類庫,自然會想到 CocoaPods。本文就用 CocoaPods 引入 PPRevealSideViewController,然後以代碼結合 Storyboard 做出抽屜效果。

一、用 CocoaPods 引入 PPRevealSideViewController

(1)在終端搜索 PPRevealSideViewController 的版本

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

(3)保存 Podfile,然後執行 pod install

二、為工程添加 pch 文件

Xcode 6 新建的工程默認沒有 pch 文件,如果想用,需要手動添加,步驟如下。

1. 新建工程時並沒有 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 並設為啟動頁:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // 獲取主視圖的導航控制器
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"NavigationController"];

    // 新建 PPRevealSideViewController,並設置根視圖(主頁面的導航視圖)
    PPRevealSideViewController *sideViewController =
        [[PPRevealSideViewController alloc] initWithRootViewController:vc];
    sideViewController.fakeiOS7StatusBarColor = [UIColor whiteColor];

    // 把 sideViewController 設置成根視圖控制器
    self.window.rootViewController = sideViewController;
    [self.window makeKeyAndVisible];
    return YES;
}

3. 在主界面推出導航頁:

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

4. 在導航頁點擊不同按鈕,切換到不同的 controller:

- (IBAction)tap1:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *one = [storyboard instantiateViewControllerWithIdentifier:@"one"];
    [self.revealSideViewController popViewControllerWithNewCenterController:one animated:YES];
}

- (IBAction)tap2:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *one = [storyboard instantiateViewControllerWithIdentifier:@"two"];
    [self.revealSideViewController popViewControllerWithNewCenterController:one animated:YES];
}

- (IBAction)tap3:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *one = [storyboard instantiateViewControllerWithIdentifier:@"three"];
    [self.revealSideViewController popViewControllerWithNewCenterController:one animated:YES];
}

- (IBAction)tap4:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *one = [storyboard instantiateViewControllerWithIdentifier:@"four"];
    [self.revealSideViewController popViewControllerWithNewCenterController:one animated:YES];
}

5. 各頁面返回主界面的代碼:

- (IBAction)tapPage:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *view = [storyboard instantiateViewControllerWithIdentifier:@"NavigationController"];
    [self.revealSideViewController popViewControllerWithNewCenterController:view animated:YES];
}

四、效果演示

以上就是本文的全部內容,希望對大家的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved