美文网首页IOS技术型
四种常用风格的抽屉视图

四种常用风格的抽屉视图

作者: 衹氏 | 来源:发表于2016-04-23 18:29 被阅读601次

    1)双向抽屉视图

    2)抽屉动画

    3)双向抽屉视图带缩放效果

    4)抽屉视图动画缩放

    #import "ApDrawerController.h"#import "AppDelegate.h"#import "leftVC.h"#import "rightVC.h"@interface ApDrawerController (){

    CGFloat _scalef;  //实时横向位移

    }

    @property (nonatomic,strong) UITableView *leftTableview;

    @property (nonatomic,assign) CGFloat leftTableviewW;

    @property (nonatomic,strong) UIView *contentView;

    @property (nonatomic,strong) UITableView *rightTableview;

    @end

    @implementation ApDrawerController

    - (void)viewDidLoad {

    [super viewDidLoad];

    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(judge) name:UIWindowDidBecomeHiddenNotification object:nil];

    }

    //-(void)judge{

    //    self.rightVC.view.hidden=YES;

    //    self.leftVC.view.hidden=NO;

    //}

    /**

    @brief 初始化侧滑控制器

    @param leftVC 左视图控制器

    @param rightVC 右视图控制器

    centerVC 中间视图控制器

    @result instancetype 初始化生成的对象

    */

    - (instancetype)initWithCenterViewController:(UIViewController *)centerVC leftViewController:(UIViewController *)leftVC RightViewController:(UIViewController *)rightVC{

    self = [super init];

    if(self){

    self.speedf = vSpeedFloat;

    self.leftVC = leftVC;

    self.centerVC = centerVC;

    self.rightVC=rightVC;

    //滑动手势

    self.pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];

    [self.centerVC.view addGestureRecognizer:self.pan];

    [self.pan setCancelsTouchesInView:YES];

    self.pan.delegate = self;

    self.leftVC.view.hidden = YES;

    self.rightVC.view.hidden=YES;

    [self addChildViewController:leftVC];

    [self.view addSubview:self.leftVC.view];

    [self addChildViewController:rightVC];//确定界面切换方式

    [self.view addSubview:self.rightVC.view];//界面可视区展现

    //蒙版

    UIView* viewl = [[UIView alloc] init];

    viewl.frame = self.leftVC.view.bounds;

    viewl.backgroundColor = [UIColor blackColor];

    viewl.alpha = 0.5;

    self.contentView = viewl;

    [self.leftVC.view addSubview:viewl];

    //蒙版

    UIView *viewr = [[UIView alloc] init];

    viewr.frame = self.rightVC.view.bounds;

    viewr.backgroundColor = [UIColor blackColor];

    viewr.alpha = 0.5;

    self.contentView = viewr;

    [self.leftVC.view addSubview:viewr];

    //获取左侧tableview????????

    for (UIView *obj in self.leftVC.view.subviews) {

    if ([obj isKindOfClass:[UITableView class]]) {

    self.leftTableview = (UITableView *)obj;

    }

    }

    //获取右侧tableview????????

    for (UIView *obj in self.rightVC.view.subviews) {

    if ([obj isKindOfClass:[UITableView class]]) {

    self.rightTableview = (UITableView *)obj;

    }

    }

    self.leftTableview.backgroundColor = [UIColor clearColor];

    self.leftTableview.frame = CGRectMake(0, 0, kScreenWidth - kMainPageDistance, kScreenHeight);

    //设置左侧tableview的初始位置和缩放系数

    self.leftTableview.transform = CGAffineTransformMakeScale(kLeftScale, kLeftScale);

    self.leftTableview.center = CGPointMake(kLeftCenterX, kScreenHeight * 0.5);

    self.rightTableview.backgroundColor = [UIColor clearColor];

    self.rightTableview.frame = CGRectMake(0, 0, kScreenWidth - kMainPageDistance, kScreenHeight);

    //设置右侧tableview的初始位置和缩放系数

    self.rightTableview.transform = CGAffineTransformMakeScale(kLeftScale, kLeftScale);

    self.rightTableview.center = CGPointMake(kLeftCenterX, kScreenHeight * 0.5);

    //        [self.view addSubview:self.centerVC.view];

    [self addChildViewController:self.centerVC];

    [self.view addSubview:self.centerVC.view];

    self.closed = YES;//初始时侧滑窗关闭

    }

    return self;

    }

    - (void) viewWillAppear:(BOOL)animated

    {

    [super viewWillAppear:animated];

    self.leftVC.view.hidden = NO;

    }

    #pragma mark - 滑动手势

    //滑动手势

    - (void) handlePan: (UIPanGestureRecognizer *)rec{

    CGPoint point = [rec translationInView:self.view];

    _scalef = (point.x * self.speedf + _scalef);

    BOOL needMoveWithTap = YES;  //是否还需要跟随手指移动

    if (((self.centerVC.view.frame.origin.x <= 0) && (_scalef <= 0)) || ((self.centerVC.view.frame.origin.x >= (kScreenWidth - kMainPageDistance )) && (_scalef >= 0)))

    {

    //边界值管控

    _scalef = 0;

    needMoveWithTap = NO;

    }

    //根据视图位置判断是左滑还是右边滑动

    if (needMoveWithTap && (rec.view.frame.origin.x >= 0) && (rec.view.frame.origin.x <= (kScreenWidth - kMainPageDistance)))

    {

    CGFloat recCenterX = rec.view.center.x + point.x * self.speedf;

    if (recCenterX < kScreenWidth * 0.5 - 2) {

    recCenterX = kScreenWidth * 0.5;

    }

    CGFloat recCenterY = rec.view.center.y;

    rec.view.center = CGPointMake(recCenterX,recCenterY);

    //scale 1.0~kMainPageScale

    CGFloat scale = 1 - (1 - kLeftMainPageScale) * (rec.view.frame.origin.x / (kScreenWidth - kMainPageDistance));

    rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,scale, scale);

    [rec setTranslation:CGPointMake(0, 0) inView:self.view];

    CGFloat leftTabCenterX = kLeftCenterX + ((kScreenWidth - kMainPageDistance) * 0.5 - kLeftCenterX) * (rec.view.frame.origin.x / (kScreenWidth - kMainPageDistance));

    //        NSLog(@"%f",leftTabCenterX);

    //leftScale kLeftScale~1.0

    CGFloat leftScale = kLeftScale + (1 - kLeftScale) * (rec.view.frame.origin.x / (kScreenWidth - kMainPageDistance));

    self.leftTableview.center = CGPointMake(leftTabCenterX, kScreenHeight * 0.5);

    self.leftTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity, leftScale,leftScale);

    //tempAlpha kLeftAlpha~0

    CGFloat tempAlpha = kLeftAlpha - kLeftAlpha * (rec.view.frame.origin.x / (kScreenWidth - kMainPageDistance));

    self.contentView.alpha = tempAlpha;

    }

    else

    {//**********************************手势范围************************************************

    //超出范围,

    if (self.centerVC.view.frame.origin.x < 0)

    {

    [self closeLeftView];

    _scalef = 0;

    }else if (self.centerVC.view.frame.origin.x >0&&self.centerVC.view.frame.origin.x<(kScreenWidth - kMainPageDistance)){

    //            self.rightVC.view.hidden=YES;

    //            self.leftVC.view.hidden=NO;

    //

    }else if (self.centerVC.view.frame.origin.x > (kScreenWidth - kMainPageDistance)){

    [self openLeftView];//self.centerVC.view仿射变换

    _scalef = 0;

    }

    }

    //手势结束后修正位置,超过约一半时向多出的一半偏移

    if (rec.state == UIGestureRecognizerStateEnded) {

    if (fabs(_scalef) > vCouldChangeDeckStateDistance)

    {

    if (self.closed)

    {

    [self openLeftView];

    }

    else

    {

    [self closeLeftView];

    }

    }

    else

    {

    if (self.closed)

    {

    [self closeLeftView];

    }

    else

    {

    [self openLeftView];

    }

    }

    _scalef = 0;

    }

    }

    #pragma mark - 单击手势

    -(void)handeTap:(UITapGestureRecognizer *)tap{

    if ((!self.closed) && (tap.state == UIGestureRecognizerStateEnded))

    {

    [UIView beginAnimations:nil context:nil];

    tap.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

    tap.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);

    self.closed = YES;

    self.leftTableview.center = CGPointMake(kLeftCenterX, kScreenHeight * 0.5);

    self.leftTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity,kLeftScale,kLeftScale);

    self.contentView.alpha = kLeftAlpha;

    [UIView commitAnimations];

    _scalef = 0;

    [self removeSingleTap];

    }

    }

    #pragma mark - 修改视图位置

    /**

    @brief 关闭左视图

    */

    - (void)closeLeftView

    {

    [UIView beginAnimations:nil context:nil];

    self.centerVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

    self.centerVC.view.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);

    self.closed = YES;

    self.leftTableview.center = CGPointMake(kLeftCenterX, kScreenHeight * 0.5);

    self.leftTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity,kLeftScale,kLeftScale);

    self.contentView.alpha = kLeftAlpha;

    [UIView commitAnimations];

    [self removeSingleTap];

    }

    /**

    @brief 关闭右视图

    */

    - (void)closeRightView

    {

    [UIView beginAnimations:nil context:nil];

    self.centerVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

    self.centerVC.view.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);

    self.closed = YES;

    self.rightTableview.center = CGPointMake(kLeftCenterX, kScreenHeight * 0.5);

    self.rightTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity,kLeftScale,kLeftScale);

    self.contentView.alpha = kLeftAlpha;

    [UIView commitAnimations];

    [self removeSingleTap];

    }

    /**

    @brief 打开左视图

    */

    - (void)openLeftView

    {

    [UIView beginAnimations:nil context:nil];

    //*************************界面展现布局效果****************************************************

    self.centerVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,kLeftMainPageScale,kLeftMainPageScale);

    self.centerVC.view.center = kLeftBarButtonItemMainPageCenter;

    //*************************界面展现布局效果****************************************************

    self.closed = NO;

    self.leftTableview.center = CGPointMake((kScreenWidth - kMainPageDistance) * 0.5, kScreenHeight * 0.5);

    self.leftTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

    self.contentView.alpha = 0;

    self.rightVC.view.hidden=YES;

    self.leftVC.view.hidden=NO;

    [(leftVC*)self.leftVC setCATransitionWithMainType:CameraIrisHollowOpen SubType:CATransitionFromRight];

    [UIView commitAnimations];

    [self disableTapButton];

    }

    /**

    @brief 打开右视图

    */

    - (void)openRightView

    {

    [UIView beginAnimations:nil context:nil];

    //*************************界面展现布局效果****************************************************

    self.centerVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,kRightMainPageScale,kLeftMainPageScale);

    self.centerVC.view.center =kRightBarButtonItemMainPageCenter;

    //*************************界面展现布局效果****************************************************

    self.closed = NO;

    self.rightTableview.center = CGPointMake((kScreenWidth - kMainPageDistance) * 0.5, kScreenHeight * 0.5);

    self.rightTableview.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);

    self.contentView.alpha = 0;

    self.rightVC.view.hidden=NO;

    self.leftVC.view.hidden=YES;

    [(rightVC*)self.rightVC setCATransitionWithMainType:OglFlip SubType:CATransitionFromRight];

    [UIView commitAnimations];

    [self disableTapButton];

    }

    #pragma mark - 行为收敛控制

    - (void)disableTapButton

    {

    for (UIButton *tempButton in [_centerVC.view subviews])

    {

    [tempButton setUserInteractionEnabled:NO];

    }

    //单击

    if (!self.sideslipTapGes)

    {

    //单击手势

    self.sideslipTapGes= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handeTap:)];

    [self.sideslipTapGes setNumberOfTapsRequired:1];

    [self.centerVC.view addGestureRecognizer:self.sideslipTapGes];

    self.sideslipTapGes.cancelsTouchesInView = YES;  //点击事件盖住其它响应事件,但盖不住Button;

    }

    }

    //关闭行为收敛

    - (void) removeSingleTap

    {

    for (UIButton *tempButton in [self.centerVC.view  subviews])

    {

    [tempButton setUserInteractionEnabled:YES];

    }

    [self.centerVC.view removeGestureRecognizer:self.sideslipTapGes];

    self.sideslipTapGes = nil;

    }

    /**

    *  设置滑动开关是否开启

    *

    *  @param enabled YES:支持滑动手势,NO:不支持滑动手势

    */

    - (void)setPanEnabled: (BOOL) enabled

    {

    [self.pan setEnabled:enabled];

    }

    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{

    return YES;

    }

    - (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch {

    if(self.beginSlide != nil)

    {

    self.beginSlide();

    }

    if(touch.view.tag == vDeckCanNotPanViewTag)

    {

    //        NSLog(@"不响应侧滑");

    return NO;

    }

    else

    {

    //        NSLog(@"响应侧滑");

    return YES;

    }

    }

    @end

    @implementation UIViewController (ApDrawerController)

    - (ApDrawerController *)apDrawerCtrl

    {

    if([self.parentViewController isKindOfClass:[ApDrawerController class]]){

    return (ApDrawerController*)self.parentViewController;

    }

    else if([self.parentViewController isKindOfClass:[UINavigationController class]] &&

    [self.parentViewController.parentViewController isKindOfClass:[ApDrawerController class]]){

    return (ApDrawerController *)self.parentViewController.parentViewController;

    }

    else{

    return nil;

    }

    }

    @end

    相关文章

      网友评论

        本文标题:四种常用风格的抽屉视图

        本文链接:https://www.haomeiwen.com/subject/oftfrttx.html