Monotouch:如何使用NavigationController.PushViewController更改视图更改的默认动画

时间:2012-08-20 13:24:54

标签: animation xamarin.ios views pushviewcontroller

我在我的应用中使用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上的平移)或制作自定义动画?

3 个答案:

答案 0 :(得分:1)

我相信您需要将对PushViewController的调用更改为不动画以避免默认动画开始。

this.NavigationController.PushViewController(someUIViewController,TRUE);

应该是

this.NavigationController.PushViewController(someUIViewController,FALSE);

答案 1 :(得分:0)

同样的问题。对此没有正常的解决方案。 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);

答案 2 :(得分:0)

//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);