欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

iOS开发实现简单抽屉效果,

来源: 开发者 投稿于  被查看 42829 次 评论:288

iOS开发实现简单抽屉效果,


本文实例为大家分享了iOS实现简单抽屉效果的具体代码,供大家参考,具体内容如下

抽屉效果的原理:其实就是把两个子控制器添加到一个RootViewController中,将子控制器的view添加到RootViewController的view上,然后改变子控制器view的frame实现抽屉的效果。

下面直接看看我自己写的一个小demo。

RootViewController.h

//两个子控制器leftView和midView
@property(nonatomic,weak)UIViewController *leftView;
@property(nonatomic,weak)UIViewController *midView;

RootViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    //将leftView和midView添加到self中作为子控制器。将他们的view添加到self.view中
    [self addChildViewController:self.leftView];
    [self.view addSubview:self.leftView.view];
    [self addChildViewController:self.midView];
    [self.view addSubview:self.midView.view];

    //设置一个按钮点击实现抽屉效果
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    leftButton.frame = CGRectMake(0, 50, 150, 150);
    [leftButton addTarget:self action:@selector(leftButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setTitle:@"left" forState:UIControlStateNormal];
    [self.midView.view addSubview:leftButton];

}

-(void)leftButtonPressed
{
    //判断抽屉是否是展开状态
    if (self.midView.view.frame.origin.x == 0) {

        //通过动画实现view.fram的改变
        [UIView animateWithDuration:0.3 animations:^{
            /*  W  H  屏幕实际大小宏
             * #define ScreenWidth [UIScreen mainScreen].bounds.size.width
             * #define ScreenHeight [UIScreen mainScreen].bounds.size.height
            */
            self.leftView.view.frame = CGRectMake(0, 0, W, H);
            self.midView.view.frame = CGRectMake(200, 50, W, H-50*2);

        } completion:^(BOOL finished) {
        }];

    }else{

        [UIView animateWithDuration:0.3 animations:^{

            self.midView.view.frame = CGRectMake(0, 0, W, H);

        } completion:^(BOOL finished) {
        }];
    }
}

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    LeftViewController *leftView = [[LeftViewController alloc] init];
    MidViewController *midView = [[MidViewController alloc]init];
    RootViewController *rootView = [[RootViewController alloc]init];
    rootView.leftView = leftView;
    rootView.midView = midView;
    self.window.rootViewController = rootView;
    [self.window makeKeyAndVisible];
    return YES;
}

运行代码,效果图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持3672js教程。

您可能感兴趣的文章:
  • iOS开发之路--仿网易抽屉效果
  • IOS中MMDrawerController第三方抽屉效果的基本使用示例
  • iOS实现简单的抽屉效果
  • iOS实现侧拉栏抽屉效果
  • IOS中Swift仿QQ最新版抽屉侧滑和弹框视图
  • iOS实现左右拖动抽屉效果
  • IOS实现点击滑动抽屉效果
  • ios仿侧边抽屉效果实现代码
  • iOS实现简单抽屉效果
  • iOS实现简易抽屉效果、双边抽屉效果

用户评论