TAG

RSS订阅

收藏本站

设为首页

当前位置:主页 > 移动开发 > IOS开发 >

iOS iPhone开发之抽屉效果实现

发布时间:2014-12-23 00:20 类别:IOS开发

说道抽屉效果在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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- (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,并设置根视图(主页面的<span id="2_nwp" style="width: auto; height: auto; float: none; "><a id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F5353%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=00007110_cpr&k=%B5%BC%BA%BD&k0=%C8%ED%BC%FE&kdi0=8&k1=%BF%D8%D6%C6%C6%F7&kdi1=8&k2=%C8%ED%BC%FE%BF%AA%B7%A2&kdi2=1&k3=%B5%BC%BA%BD&kdi3=1&k4=%C6%BB%B9%FB&kdi4=8&sid=cd375c5af5c4bdbd&ch=0&tu=u1704338&jk=8113038773182046&cf=29&fv=11&stid=9&urlid=0&luki=4&seller_id=1&di=128" target="_blank" mpid="2" style="text-decoration: none; "><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">导航</span></a></span>视图)
    PPRevealSideViewController *sideViewController = [[PPRevealSideViewController alloc] initWithRootViewController:vc];
    
    sideViewController.fakeiOS7StatusBarColor = [UIColor whiteColor];
    
    //把sideViewController设置成根视图<span id="3_nwp" style="width: auto; height: auto; float: none; "><a id="3_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?rs=1&u=http%3A%2F%2Fwww%2Edaxueit%2Ecom%2Farticle%2F5353%2Ehtml&p=baidu&c=news&n=10&t=tpclicked3_hc&q=00007110_cpr&k=%BF%D8%D6%C6%C6%F7&k0=%C8%ED%BC%FE&kdi0=8&k1=%BF%D8%D6%C6%C6%F7&kdi1=8&k2=%C8%ED%BC%FE%BF%AA%B7%A2&kdi2=1&k3=%B5%BC%BA%BD&kdi3=1&k4=%C6%BB%B9%FB&kdi4=8&sid=cd375c5af5c4bdbd&ch=0&tu=u1704338&jk=8113038773182046&cf=29&fv=11&stid=9&urlid=0&luki=2&seller_id=1&di=128" target="_blank" mpid="3" style="text-decoration: none; "><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">控制器</span></a></span>
    self.window.rootViewController = sideViewController;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

    3.在主界面使用PPRevealSideViewController来推出导航

1
2
3
4
5
6
- (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.在导航页点击不同的按钮使用PPRevealSideViewController跳转到不同的controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- (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.各个页面返回到主界面的代码如下:

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

  四.到此效果实现完毕,下面是效果图:

 
猜你会喜欢....

Copyright © 2015 www.wahenzan.com 哇!很赞 版权所有 浙ICP备14030256号-1 Power by DedeCms

浙公网安备 33010602005986号

声明:本站所有文章除标明原创外,均来自网络转载,版权归原作者所有,如果有侵犯到您的权益,请联系本站删除 网站管理员:758763728

360网站安全检测平台