【问题标题】:Monotouch: How to change default animation of view change with NavigationController.PushViewControllerMonotouch:如何使用 NavigationController.PushViewController 更改视图更改的默认动画
【发布时间】:2012-08-20 13:24:53
【问题描述】:

我在我的应用程序中使用 NavigationController 进行导航。当 PushViewController 启动时(someUIViewController,true),它会使用默认动画更改视图(子控制器从右向左移动)。我怎样才能改变这个动画?现在我正在使用这个:

this.NavigationController.PushViewController(someUIViewController,true);
        UIView.BeginAnimations(null);
        UIView.SetAnimationDuration(0.4);
        UIView.SetAnimationTransition(UIViewAnimationTransition.FlipFromRight, NavigationController.View,true);
        UIView.CommitAnimations();

但我仅限于四种 UIViewAnimationTransition 类型。我找到了我需要的(从下到上的视图外观):

this.NavigationController.PushViewController(someUIViewController,true);
        var theAnimation = CABasicAnimation.FromKeyPath("transform.translation.y");
        theAnimation.Duration = 0.3f;
        theAnimation.From = NSNumber.FromFloat(this.View.Frame.Height);
        theAnimation.To = NSNumber.FromFloat(0f);
        NavigationController.View.Layer.AddAnimation(theAnimation, "animate");
        NavigationController.View.Layer.AnimationForKey("animate");

但是当 CABasicAnimation 启动时,默认动画(从左到右移动到父控制器)也会启动。结果是错误的组合。我怎样才能只运行一个(在 y 上平移)或制作自定义动画?

【问题讨论】:

    标签: animation xamarin.ios views pushviewcontroller


    【解决方案1】:

    我认为您需要将对 PushViewController 的调用更改为不设置动画以避免默认动画启动。

    this.NavigationController.PushViewController(someUIViewController,true);

    应该是

    this.NavigationController.PushViewController(someUIViewController,false);

    【讨论】:

    • 谢谢,已经找到了=) 但是解决了组合动画的问题,切换到父viewcontroller时不改变默认动画
    【解决方案2】:

    同样的问题。对此没有正常的解决方案。 PushViewController 受标准动画(UIViewAnimationTransition 的类型)的限制。如果你想改变它们,试试这个:

    NavigationController.PushViewController(screen, false); 
    var theAnimation = CABasicAnimation.FromKeyPath("transform.translation.x"); 
    theAnimation.Duration = 0.6f; 
    theAnimation.From = NSNumber.FromFloat(-NavigationController.View.Frame.Width); 
    theAnimation.To = NSNumber.FromFloat(0f); 
    NavigationController.View.Layer.AddAnimation(theAnimation, "animate"); 
    NavigationController.View.Layer.AnimationForKey("animate"); 
    

    但这并不完美,尝试一下,你就会明白为什么。你也可以使用 PresentViewController。它在 UIModalTransitionStyle 中有一些其他标准动画:

    this.NavigationController.PresentViewController(new UINavigationController(screen){ 
            ModalTransitionStyle= UIModalTransitionStyle.CoverVertical, 
    }, true,null);
    

    【讨论】:

      【解决方案3】:
      //Allows a UINavigationController to push using a custom animation transition
      public static void PushControllerWithTransition(this UINavigationController 
        target, UIViewController controllerToPush, 
        UIViewAnimationOptions transition)
      {
        UIView.Transition(target.View, 0.75d, transition, delegate() {
          target.PushViewController(controllerToPush, false);
        }, null);
      }
      
      //Allows a UINavigationController to pop a using a custom animation 
      public static void PopControllerWithTransition(this UINavigationController 
        target, UIViewAnimationOptions transition)
      {
        UIView.Transition(target.View, 0.75d, transition, delegate() {
          target.PopViewControllerAnimated(false);
        }, null);         
      }
      

      // 有了这些扩展,在带有翻转动画的控制器之间移动现在就像这样微不足道:

      //Pushing someController to the top of the stack
      NavigationController.PushControllerWithTransition(someController, 
        UIViewAnimationOptions.TransitionFlipFromLeft);
      
      //Popping the current controller off the top of the stack
      NavigationController.PopControllerWithTransition(
        UIViewAnimationOptions.TransitionFlipFromRight);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-31
        • 2013-09-13
        • 2012-02-07
        • 1970-01-01
        • 1970-01-01
        • 2017-01-28
        • 1970-01-01
        相关资源
        最近更新 更多