【问题标题】:iPhone Pushing View Controller in a left directioniPhone向左推视图控制器
【发布时间】:2010-11-08 22:14:37
【问题描述】:

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

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

【问题讨论】:

    标签: iphone view uinavigationcontroller uinavigationbar transition


    【解决方案1】:

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

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

    【讨论】:

    • 那么以正常方式加载视图控制器,然后获取视图的句柄,然后移动它?我还必须手动更改导航栏按钮
    • 对。正常加载视图控制器,将 currentView 移出屏幕并移入 newView。
    • 这有效(您必须将 -= 替换为 +=)才能从左侧滑入。然而,这意味着为每个视图手动执行所有按钮的前后移动。多么痛苦。
    • 关于如何设置按钮样式以使其成为导航中通常使用的“尖”类型的任何想法? (后退按钮)
    • 除了使用导航控制器或您自己的图像之外,我还没有找到任何方法来获取导航控制器的尖按钮。
    【解决方案2】:

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

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

    [self.navigationController popViewController];
    

    或任何正确的方法(我认为这非常接近它的方式)。这将使您可以使用所需的动画向下移动导航堆栈,如果您的导航堆栈上有一定数量的视图,则此方法有效。

    【讨论】:

      【解决方案3】:

      要获得“尖”型按钮,您需要使用不同的方法。

      在您的 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];
      

      【讨论】:

        【解决方案4】:

        当我们推动视图控制器时,我已经改变了动画方向。 您可以在此处更改动画类型 [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];
        

        【讨论】:

        • 它使以前的视图控制器变黑,这是坏事。
        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

          正如 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];
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-02-02
            • 1970-01-01
            • 2012-05-09
            • 1970-01-01
            • 1970-01-01
            • 2016-10-22
            • 2010-12-11
            相关资源
            最近更新 更多