zoukankan      html  css  js  c++  java
  • (九十四)集成PKRevealController实现左右抽屉视图

    使用PKRevealController可以实现类似于QQ等软件的左右抽屉视图,拖出的视图分为leftView和rightView,分别取自View的左半部分和右半部分,因此,根据不同的需求,可以选择使用一个View作为leftView和rightView,也可以指定两个View。

    下面介绍这个框架的基本使用步骤。

    ①从github下载源码:PKRevealController

    ②导入Source/PKRevealController文件夹到工程。

    ③为了方便,下面以代码的方式创建窗口的根控制器,利用storyboard创建左右抽屉视图。

    1.要利用代码创建根控制器,首先在工程设置中去掉Main Interface中的Main。

    2.在AppDelegate中,导入PKRevealController.h,创建根控制器,在这里,默认展示的控制器被称为frontView,左右抽屉分别称为leftView和rightView。

    ④PKRevealController的类方法revealControllerWithFrontViewController方法有两个,一个是仅创建单个抽屉,一个是两个一起创建,只要传入UIViewController即可,这里演示的是通过leftView.storyboard创建的视图同时作为leftView和rightView。

    注意最后根控制器应该是revealController

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        
        UIViewController *mainVc = [[UIViewController alloc] init];
        mainVc.view.backgroundColor = [UIColor whiteColor];
        
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"leftView" bundle:nil];
        UIViewController *leftVc = sb.instantiateInitialViewController;
        UIViewController *rightVc = leftVc;
        
        self.revealVc = [PKRevealController revealControllerWithFrontViewController:mainVc leftViewController:leftVc rightViewController:rightVc];
        
        self.revealVc.delegate = self;
        
        self.revealVc.animationDuration = 0.25;
        
        self.window.rootViewController = self.revealVc;
        
        [self.window makeKeyAndVisible];
        
        return YES;
        
    }
    需要注意的是,storyboard的instantiateInitialViewController方法每调用一次,就会创建一个新的UIViewController,如果要共用一个,不能调用两次。

    ⑤设置代理的目的是监听当前的焦点位置,是leftView、frontView还是rightView;设置动画间隔是为了设置抽屉展开和收起的动画速度,快一些效果更好。

    一个下图这样布局的storyboard在leftView和rightView上分别展示如下:

           


    ⑥实现代理方法可以监听状态的改变:

    - (void)revealController:(PKRevealController *)revealController didChangeToState:(PKRevealControllerState)state{
        
        switch (state) {
            case PKRevealControllerShowsFrontViewController:
                NSLog(@"展示主窗口%@",revealController.frontViewController);
                break;
            case PKRevealControllerShowsLeftViewController:
                NSLog(@"展示左窗口%@",revealController.leftViewController);
                break;
            case PKRevealControllerShowsRightViewController:
                NSLog(@"展示右窗口%@",revealController.rightViewController);
                break;
            default:
                break;
        }
        
    }

    ⑦通过下面两个方法实现抽屉加长、回缩。

    /**
     让抽屉进入展示模式,也就是加长一段
     
     @param animated 是否有动画效果
     @param completion 执行完毕后的回调
     */
    - (void)enterPresentationModeAnimated:(BOOL)animated
                               completion:(PKDefaultCompletionHandler)completion;
    
    /**
     如果在展示模式下,则回到正常的抽屉或者退出展示。
    
     @param entirely 如果传YES,会将整个抽屉退出,传NO则只退出展示模式
     @param animated 是否有动画效果
     @param completion 执行完毕后的回调
     */
    - (void)resignPresentationModeEntirely:(BOOL)entirely
                                  animated:(BOOL)animated
                                completion:(PKDefaultCompletionHandler)completion;
    要判断当前所属模式,使用下面的成员属性:

    @property (nonatomic, readonly) BOOL isPresentationModeActive;
    下面的代码通过给leftView和rightView重写触摸事件,通过拿到revealViewController来更改抽屉模式。

    注意应该在AppDelegate中将revealController暴露出来才能获取到。

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    
        AppDelegate *delegate = [UIApplication sharedApplication].delegate;
        PKRevealController *revealVc = [delegate sharedRevealVc];
        if([revealVc isPresentationModeActive]){
            [revealVc resignPresentationModeEntirely:NO animated:YES completion:nil];
        }else{
            [revealVc enterPresentationModeAnimated:YES completion:nil];
        }
        
    }

  • 相关阅读:
    Codeforces 916E Jamie and Tree (换根讨论)
    BZOJ 3083 遥远的国度 (换根讨论 + 树链剖分)
    Codeforces 703D Mishka and Interesting sum(离线 + 树状数组)
    ECNU 3480 没用的函数 (ST表预处理 + GCD性质)
    HDU 4343 Interval query(贪心 + 倍增)
    Codeforces 147B Smile House(DP预处理 + 倍增)
    HDU 4870 Rating (高斯消元)
    TopCoder SRM 301 Div2 Problem 1000 CorrectingParenthesization(区间DP)
    Hrbust 2320 OX (博弈)
    Hrbust 2319 Number Game(贪心)
  • 原文地址:https://www.cnblogs.com/aiwz/p/6154100.html
Copyright © 2011-2022 走看看