我正在尋找一種在iOS中實現與Android Navigation Drawer儘可能接近的方法。適用於iOS的類似Android的導航抽屜?
這基本上是一個從左側滑過當前視圖的菜單面板。
我看過使用ECSlidingViewController,MMDrawerController,etc。但我真的想要一個抽屜,當前視圖的頂部上出現,而不是當前視圖滑動顯示下面的菜單的Facebook應用程序。
如何實現我所需的功能?
我正在尋找一種在iOS中實現與Android Navigation Drawer儘可能接近的方法。適用於iOS的類似Android的導航抽屜?
這基本上是一個從左側滑過當前視圖的菜單面板。
我看過使用ECSlidingViewController,MMDrawerController,etc。但我真的想要一個抽屜,當前視圖的頂部上出現,而不是當前視圖滑動顯示下面的菜單的Facebook應用程序。
如何實現我所需的功能?
您需要一個SlideOverViewController,它具有與您想要重疊的寬度相同的表格視圖,將視圖的背景色設置爲清晰的顏色(以實現透明度)。
在您的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];}
可能將不得不推出自己的。這並不是特別困難,將一個滑動手勢識別器綁定到一個方法上,該方法將新視圖的框架從屏幕外移動到位置。 –
很明顯,它可以做到,我有一個工作代碼,但它太大,不能在這裏寫,如果你正在尋找我可以提供的大綱? – ManicMonkOnMac
@ManicMonkOnMac一些指針將非常感謝 –