1

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

4

3 回答 3

0

Was same problem. There is no normal solution for this. PushViewController is limited by the standard animation(type of UIViewAnimationTransition). If you want to change them, try this:

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

But it is not perfect, try and u see why. Also u can use PresentViewController. It have some other standart animations in UIModalTransitionStyle:

this.NavigationController.PresentViewController(new UINavigationController(screen){ 
        ModalTransitionStyle= UIModalTransitionStyle.CoverVertical, 
}, true,null);
于 2014-02-28T08:42:29.793 回答
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);
于 2014-02-28T08:51:54.473 回答
0

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

this.NavigationController.PushViewController(someUIViewController,true);

应该

this.NavigationController.PushViewController(someUIViewController,false);

于 2012-10-12T22:31:01.630 回答