抽屉效果

Posted ZhangDreamK

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了抽屉效果相关的知识,希望对你有一定的参考价值。

这篇博客是自己写出的抽屉效果
基本上看出抽屉效果怎么实现的
虽然没有上一篇的效果炫酷 但是这是自己实现的效果
也是非常值得学习的

#import "ContentViewController.h"
#import "LeftViewController.h"
#import "CenterViewController.h"
#import "RightViewController.h"

// 屏幕尺寸
#define ScreenSize [UIScreen mainScreen].bounds.size

@interface ContentViewController ()
- (IBAction)leftSideMenuAction:(UIButton *)sender;
- (IBAction)rightSideMenuAction:(UIButton *)sender;

@end

@implementation ContentViewController

- (void)viewDidLoad 
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addViewControllers];


- (void)didReceiveMemoryWarning 
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

*/
// 添加子视图控制器
- (void)addViewControllers

    // 获取视图控制器
    // 获取UIStoryboard对象
    UIStoryboard * sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    // 实例化视图控制器
    LeftViewController * leftVC = [sb instantiateViewControllerWithIdentifier:@"LeftViewController"];
    CenterViewController * centerVC = [sb instantiateViewControllerWithIdentifier:@"CenterViewController"];
    RightViewController * rightVC = [sb instantiateViewControllerWithIdentifier:@"RightViewController"];
    // 添加子视图控制器
    [self addChildViewController:leftVC];
    [self addChildViewController:centerVC];
    [self addChildViewController:rightVC];

    // 添加子视图控制器的视图到当前视图控制器的视图上,作为子视图使用
    [self.view addSubview:leftVC.view];
    [self.view addSubview:centerVC.view];
    [self.view addSubview:rightVC.view];
    [self.view sendSubviewToBack:centerVC.view];

    // 修改左侧视图的frame
    leftVC.view.frame = CGRectMake(-ScreenSize.width, 0, ScreenSize.width, ScreenSize.height);
    // 修改右侧视图的frame
    rightVC.view.frame = CGRectMake(ScreenSize.width, 0, ScreenSize.width, ScreenSize.height);




- (IBAction)leftSideMenuAction:(UIButton *)sender 
    NSArray * childVCs = self.childViewControllers;
    UIViewController * leftVC = childVCs[0];
    UIViewController * centerVC = childVCs[1];
    [UIView animateWithDuration:1 animations:^
        leftVC.view.frame = CGRectMake(0, 0, ScreenSize.width, ScreenSize.height);
        centerVC.view.frame = CGRectMake(ScreenSize.width / 2, 0, ScreenSize.width, ScreenSize.height);
        [self.view bringSubviewToFront:centerVC.view];
    ];


- (IBAction)rightSideMenuAction:(UIButton *)sender 

以上是关于抽屉效果的主要内容,如果未能解决你的问题,请参考以下文章

抽屉效果

DrawerLayout(抽屉效果)

抽屉效果

动画的抽屉效果

Wpf 抽屉效果

实现侧边抽屉效果-YRSideViewController