SlideOverViewController

SlideOverViewController

Closed. This question does not meet Stack Overflow guidelines。它当前不接受答案。












想要改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。

7年前关闭。



Improve this question




我正在寻找一种在iOS中实现与Android Navigation Drawer尽可能接近的功能的方法。

基本上,这是一个菜单面板,可从左侧滑入当前 View 。

我看过使用ECSlidingViewController,MMDrawerController,etc。但我确实希望在当前 View 顶部显示一个抽屉,而不像Facebook应用程序在其中滑动当前 View 以显示其下方的菜单。

如何实现所需的功能?

最佳答案

您将需要一个SlideOverViewController,该控件的表格 View 的宽度要与您要重叠的宽度相同,然后将 View 的背景色设置为透明色(以实现透明性)。

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

    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,请使用:
    [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;
}];

要隐藏菜单,请使用:
        [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];
    }];

在您的SLideOverViewController中,

添加gestureRecognizers:
    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];}

10-08 05:55