首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >像安卓一样的iOS导航抽屉?

像安卓一样的iOS导航抽屉?
EN

Stack Overflow用户
提问于 2013-10-30 00:15:33
回答 1查看 10.4K关注 0票数 17

我正在寻找一种方法来在iOS中实现一些尽可能接近安卓Navigation Drawer的东西。

这基本上是一个菜单面板,从左侧滑动到当前视图上。

我看过如何使用ECSlidingViewController、MMDrawerController、etc。但我真的想要一个出现在当前视图顶部的抽屉,而不是像Facebook应用程序那样,当前视图滑动显示下面的菜单。

如何实现我想要的功能?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-30 02:22:14

您可能需要一个SlideOverViewController,其表格视图的宽度与您希望重叠的宽度相同,请将视图的背景色设置为透明颜色(以实现透明度)。

在MainViewController中,初始化并添加SlideOverViewController。

代码语言:javascript
复制
    self.slideOverViewController = [[SlideOverViewController alloc] init];
    self.slideOverViewController.view.frame = CGRectMake(-self.myNavigationController.view.frame.size.width, 0, self.myNavigationController.view.frame.size.width, self.view.frame.size.height);
    self.slideOverViewController.delegate = self;

要激活slideOverMenu,请使用以下命令:

代码语言:javascript
复制
    [self.slideOverViewController.view setHidden:NO];

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

    [UIView animateWithDuration:kMenuAnimationDuration animations:^{

    self.slideOverViewController.view.frame = CGRectMake(0, 0, self.slideOverViewController.view.frame.size.width, self.slideOverViewController.view.frame.size.height);

    [UIView animateWithDuration:kMenuAnimationDuration animations:^{


    }completion:^(BOOL finished){

    }];

}completion:^(BOOL finished){
    self.mainMenuDisplay = YES;
}];

要隐藏菜单,请使用:

代码语言:javascript
复制
        [UIView animateWithDuration:kMenuAnimationDuration animations:^{

        self.slideOverViewController.view.frame = CGRectMake(-self.myNavigationController.view.frame.size.width - 80, 0, self.myNavigationController.view.frame.size.width, self.myNavigationController.view.frame.size.height);

    }completion:^(BOOL finished){

        self.mainMenuDisplay = NO;
        [self.slideOverViewController.view setHidden:YES];
    }];

在you SLideOverViewController中,

添加gestureRecognizers:

代码语言:javascript
复制
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestures:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];



  - (void) handlePanGestures : (UIPanGestureRecognizer *) sender
{
if (self.view.frame.origin.x > 0) {
    return;
}

[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
    self.firstX = [[sender view] center].x;
    self.firstY = [[sender view] center].y;
}

translatedPoint = CGPointMake(self.firstX+translatedPoint.x, self.firstY);

if (translatedPoint.x > self.view.frame.size.width/2) {
    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    return;
}

[[sender view] setCenter:translatedPoint];

if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded || [sender state] == UIGestureRecognizerStateCancelled || [sender state] == UIGestureRecognizerStateFailed) {

    CGFloat velocityX = (0.2*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
    CGFloat finalX = translatedPoint.x + velocityX;
    CGFloat finalY = self.firstY;

    if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
        if (finalX < 0) {
            //finalX = 0;
        } else if (finalX > 768) {
            //finalX = 768;
        }

        if (finalY < 0) {
            finalY = 0;
        } else if (finalY > 1024) {
            finalY = 1024;
        }
    } else {
        if (finalX < 0) {
            //finalX = 0;
        } else if (finalX > 1024) {
            //finalX = 768;
        }

        if (finalY < 0) {
            finalY = 0;
        } else if (finalY > 768) {
            finalY = 1024;
        }
    }

    CGFloat animationDuration = (ABS(velocityX)*.0002)+.2;

    [self animateToPoint:finalX yPos:finalY withAnimationDuration:animationDuration];
}
}
}
}

- (void) animateToPoint : (CGFloat) finalX yPos : (CGFloat) finalY withAnimationDuration : (CGFloat) animationDuration {
if (self.view.frame.origin.x < -90) {
    [self handleCloseSlidingMenuViewController];

} else {
    [self handleShowSlidingMenuView : finalX yPos:finalY withAnimationDuration:animationDuration];
}}

- (void) handleShowSlidingMenuView : (CGFloat) finalX
                          yPos : (CGFloat) finalY
         withAnimationDuration : (CGFloat) animationDuration{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidFinish)];
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];}
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19663692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档