【问题标题】:UINavigationController Custom Transition AnimationsUINavigationController 自定义过渡动画
【发布时间】:2010-02-24 09:33:59
【问题描述】:

我有一个 UINavigationController,它有一个可见的导航栏和工具栏。推送控制器(同一个控制器)时,我只想为内容部分设置动画(例如具有滑动效果),并保持 NavigationBar 和 Toolbar 不动画/恒定。任何人都可以就这样做的干净方式提供任何建议吗?谢谢。

【问题讨论】:

    标签: iphone uinavigationcontroller


    【解决方案1】:

    我会手动移动视图并使用动画块对其进行动画处理:

    // Get position of old view controller
    CGPoint center = [oldViewController.view center];
    
    // Position new view controller to the right of old one
    [newViewController.view setCenter:CGPointMake(center.x + 320, center.y)];
    
    // Animate change
    [UIView beginAnimations:@"myAnimation" context:NULL];
    
    // Move old view off screen
    [oldViewController.view setCenter:CGPointMake(center.x - 320, center.y)];
    
    // Move new view onto screen
    [newViewController.view setCenter:center];
    
    // Set animation delegate to self so that we can detect when animation is complete
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationComplete:finished:target:)];
    
    // Finish animation block
    [UIView commitAnimations];
    

    如果您需要使用导航控制器实际推送新的视图控制器,您需要确保在动画完成后执行此操作,否则将不会显示旧视图。为此,您需要使用 setAnimationDelegate 方法来获取动画何时完成的通知,并传入要调用的选择器方法。在同一个类中实现上面的代码和下面的方法:

    (void)animationComplete:(NSString *)animationId 
                   finished:(BOOL)finished 
                     target:(UIView *)target
    {
      if([animationId isEqualToString:@"myAnimation"])
      {
        // Push viewcontroller here
      }
    }
    

    【讨论】:

    • 谢谢。它几乎可以工作,除了 oldViewController 视图在推送后消失。动画发生时,旧视图只是黑色。你知道如何在 oldViewController.view 被推送后保持可见吗?这是我为设置 oldViewController 和 newViewController 添加的代码。 CGPoint center = [self.view center]; UIViewController* oldViewController = [self.navigationController topViewController]; NewController* newViewController = [[[NewController alloc] init] autorelease]; [self.navigationController pushViewController:newViewController Animation:NO];
    • 在推入新的视图控制器之前,您需要结束动画块。即在[self.navigationController pushViewController:newViewController animated:NO]之前调用[UIView commitAnimations]
    • 感谢您的回复。我将推送移到提交之后,它仍然产生相同的结果,新视图转换为黑色/空视图。我在没有推送的情况下尝试了代码,以查看 oldView 是否过渡出来,并且它可以工作,但是一旦我引入推送,它就会消失。
    • 我知道发生了什么,您需要在动画完成后推送新的视图控制器。查看更新的答案
    • 再次感谢!不幸的是,这似乎也不起作用:(这会产生相反的效果,我看到旧的控制器滑掉了,新控制器应该在的地方是黑色的,然后新的控制器视图在它被推动后就会显示出来。问题是在我开始动画之前 newViewController 没有被添加到视图层次结构中,它只是被初始化了。它只在被推送时出现(此时动画已经完成)。我最初想做的是创建一个透明 b/w 推动的控制器和下面的控制器。那不起作用...
    【解决方案2】:

    尝试将工具栏作为子视图添加到窗口而不是视图。并限制 navigationcontroller 视图高度,以便您可以看到它的工具栏。现在,如果您按下视图控制器,导航栏和工具栏将保持无动画/停滞状态。

    【讨论】:

    • 我想过这样做,但它似乎是一个大黑客。我会试一试,然后回帖。谢谢!
    • 我试过这个,它在大多数情况下都有效。第一次尝试我调整了导航控制器的高度,但是当我按下模态控制器并关闭它时,高度总是在关闭时返回不正确(只能在 viewDidAppear 中更改高度,这会给我断断续续的动画过渡)。接下来,我没有使用导航高度,只是覆盖了添加到窗口的工具栏,但随后我与模态控制器重叠,向上滑动 b/c 窗口中的工具栏位于更高的层上,然后是导航。还不知道修复那个动画。想法?
    【解决方案3】:

    将新 UIViewController 的 navigationItem 设置为与当前 UIViewController 相同。

    newViewcontroller.navigationItem = self.navigationItem;
    [self.navigationController pushViewController:recallViewController animated:YES];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多