iPhone向左推动View Controller

时间:2009-07-08 04:13:23

标签: iphone view uinavigationcontroller uinavigationbar transition

我有一个应用程序,它有一个中心视图,每个视图都有两个视图。我想要左右两个导航栏按钮,将新的导航控制器从左侧或右侧推到视图上。

当您使用NavigationController的pushviewController:方法推送新视图来更改视图时,视图似乎从右侧滑入。如何将其更改为从左侧滑入?

6 个答案:

答案 0 :(得分:9)

当我们推动viewcontroller时,我已经完成了更改动画方向。 你可以在这里更改动画类型 [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];

答案 1 :(得分:8)

我只是移动视图而不是使用导航控制器。

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];

答案 2 :(得分:4)

我认为你不能在UINavigationControllers中明确定义滑动方向。您可以做的是从导航堆栈弹出当前视图以显示先前视图,该视图将以您想要的方式进行动画制作。但是,如果您希望显示不同的视图控制器,这可能会很复杂,具体取决于您在当前视图中执行的操作。

如果您的工作流程不是太复杂,您可以在当前视图控制器中保留对先前视图控制器的引用。根据您在当前视图中执行的操作(如选择表视图单元格),您可以更改先前视图控制器中所需的任何数据,然后调用

[self.navigationController popViewController];

或者任何正确的方法(我认为这非常接近它)。这会让你沿着你想要的动画向下移动导航堆栈,如果你的导航堆栈上有一定数量的视图,这将有效。

答案 3 :(得分:3)

到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];
}

答案 4 :(得分:0)

要获得“尖”类型按钮,您需要使用其他方法。

在AppDelegate中:

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];

答案 5 :(得分:0)

您可以继承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];

推动将从左到右,从右到左弹出