我有一个应用程序,其中心视图有两个侧面视图。我想要左右两个导航栏按钮,它们可以从左侧或右侧向视图推入新的导航控制器。
当您使用NavigationController的pushviewController:方法通过推入新视图来更改视图时,该视图似乎从右侧滑入。如何更改为从左侧滑入?
我有一个应用程序,其中心视图有两个侧面视图。我想要左右两个导航栏按钮,它们可以从左侧或右侧向视图推入新的导航控制器。
当您使用NavigationController的pushviewController:方法通过推入新视图来更改视图时,该视图似乎从右侧滑入。如何更改为从左侧滑入?
当我们推出视图控制器时,我已经改变了动画方向。您可以在此处更改动画类型 [animation setSubtype:kCATransitionFromRight];
ViewController *elementController = [[ViewController alloc] init];
// set the element for the controller
ViewController.element = element;
// push the element view controller onto the navigation stack to display it
CATransition *animation = [CATransition animation];
[[self navigationController] pushViewController:elementController animated:NO];
[animation setDuration:0.45];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
[[elementController.view layer] addAnimation:animation forKey:@"SwitchToView1"];
[elementController release];
不使用导航控制器,我会直接移动视图。
CGRect inFrame = [currentView frame];
CGRect outFrame = firstFrame;
outFrame.origin.x -= inFrame.size.width;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[newView setFrame:inFrame];
currentView setFrame:outFrame];
[UIView commitAnimations];
[self.navigationController popViewController];
或者采用正确的方法(我认为这个方法非常接近)。这将允许您使用所需的动画向下移动导航栏堆栈,仅在导航栏堆栈上有一组视图时才有效。
根据Reed Olsen的说法:您只需要连接一个按钮,该按钮启动向同一方法的幻灯片上滑,并添加一个跟踪视图是否显示的BOOL。所有您需要做的就是正确设置原点。
- (IBAction)slideMenuView
{
CGRect inFrame = [self.view frame];
CGRect outFrame = self.view.frame;
if (self.viewisHidden) {
outFrame.origin.x += inFrame.size.width-50;
self.viewisHidden = NO;
} else {
outFrame.origin.x -= inFrame.size.width-50;
self.viewisHidden = YES;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[self.menuView setFrame:inFrame];
[self.view setFrame:outFrame];
[UIView commitAnimations];
}
UITableViewController *first = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
UITableViewController *second = [[SomeOtherViewController alloc] initWithStyle:UITableViewStylePlain];
NSArray *stack = [NSArray arrayWithObjects: first, second, nil];
UINavigationController *nav = [[UINavigationController alloc] init];
[nav setViewControllers:stack animated:NO];
您可以继承RTLNavigationController:UINavigationController并重写这些函数。
- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
DummyViewController*dvc = [[DummyViewController alloc] init];
[super pushViewController:viewController animated:NO];
[super pushViewController:dvc animated:NO];
[dvc release];
[super popViewControllerAnimated:YES];
}
并且
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
UIViewController *firstViewController = [super popViewControllerAnimated:NO];
UIViewController *viewController = [super popViewControllerAnimated:NO];
[super pushViewController:viewController animated:animated];
return firstViewController;
}
在应用程序委托中:
navCon = [[RTLNavigationController alloc] init];
rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
rootViewController.contextDelegate = self;
DummyViewController *dvc = [[DummyViewController alloc]init];
[navCon pushViewController:dvc animated:NO];
[dvc release];
[navCon pushViewController:rootViewController animated:NO];
[self.window addSubview:navCon.view];
推入操作将从左到右进行,弹出操作将从右到左进行