如何在视图控制器之间进行淡入/淡出转换

故事板中的视图控制器之间是否可以淡入淡出? 或者没有过渡。

如果可能的话,它的代码是什么?

如果呈现模式视图控制器,则可以指定modalTransitionStyleUIModalTransitionStyleCrossDissolve 。 如果在故事板中使用segue执行此操作,请selectsegue的属性检查器,然后在其中指定转换样式:

在这里输入图像说明

如果以编程方式呈现视图控制器,则可以在故事板中的视图控制器之间使用“交叉解散”的“过渡”定义模态,然后让源视图控制器执行以下过程:

 [self performSegueWithIdentifier:@"presentSegue" sender:sender]; 

或者,如果您正在调用presentViewController

 UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"YourStoryboardID"]; controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentViewController:controller animated:YES completion:nil]; 

在iOS 7中,Apple提供了一项新技术,可为高度自定义的转换提供丰富而强大的控制。 有关更多信息,请参阅使用视图控制器的 WWDC 2013video自定义转场 。

但是,例如,如果您想要自定义iOS 7中的推送和popupanimation以淡入淡出,则可以为导航控制器指定一个delegate

 - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.delegate = self; } 

然后,您将实现animationControllerForOperation ,指定animation器对象的推送和popup:

 - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController*)fromVC toViewController:(UIViewController*)toVC { if (operation == UINavigationControllerOperationPush) return [[PushAnimator alloc] init]; if (operation == UINavigationControllerOperationPop) return [[PopAnimator alloc] init]; return nil; } 

你显然必须实现你的自定义推送和stream行animation,如:

 @interface PushAnimator : NSObject <UIViewControllerAnimatedTransitioning> @end @implementation PushAnimator - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext { return 0.5; } - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; [[transitionContext containerView] addSubview:toViewController.view]; toViewController.view.alpha = 0.0; [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ toViewController.view.alpha = 1.0; } completion:^(BOOL finished) { [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; }]; } @end 

 @interface PopAnimator : NSObject <UIViewControllerAnimatedTransitioning> @end @implementation PopAnimator - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext { return 0.5; } - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext { UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view]; [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ fromViewController.view.alpha = 0.0; } completion:^(BOOL finished) { [transitionContext completeTransition:![transitionContext transitionWasCancelled]]; }]; } @end 

对于自定义模态转换,也有一个类似的,但略有不同的技术(虽然如果你只是做一个脸部,你可能只是使用modalTransitionStyle讨论的modalTransitionStyle ,除非你想要使用一些其他微妙的定制)。 有关更多信息,请参阅上述使用视图控制器的自定义转换 。

底线,iOS 7的自定义转换是一个稍微复杂的,但非常强大的方式来提供对转换animation的巨大控制。

用于创buildUIStoryboard segue的Custom Segue创build子类。 例如:

 // MCFadeSegue.h #import <UIKit/UIKit.h> @interface MCFadeSegue : UIStoryboardSegue @end // MCFadeSegue.m #import <QuartzCore/QuartzCore.h> #import "MCFadeSegue.h" @implementation MCFadeSegue - (void)perform { CATransition *transition = [CATransition animation]; transition.duration = 0.5; transition.type = kCATransitionFade; [[[[[self sourceViewController] view] window] layer] addAnimation:transition forKey:kCATransitionFade]; [[self sourceViewController] presentViewController:[self destinationViewController] animated:NO completion:NULL]; } @end 

然后在MainStoryboard.storyboard中selectsegue并设置Style:Custom和Class:MCFadeSegue。

在Swift中推送/popupUIVIewController FadeIn / FadeOut

 class FadeInPushSegue: UIStoryboardSegue { var animated: Bool = true override func perform() { if var sourceViewController = self.sourceViewController as? UIViewController, var destinationViewController = self.destinationViewController as? UIViewController { var transition: CATransition = CATransition() transition.type = kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade sourceViewController.view.window?.layer.addAnimation(transition, forKey: "kCATransition") sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false) } } } class FadeOutPopSegue: UIStoryboardSegue { override func perform() { if var sourceViewController = self.sourceViewController as? UIViewController, var destinationViewController = self.destinationViewController as? UIViewController { var transition: CATransition = CATransition() transition.duration = 0.4 transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) transition.type = kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade sourceViewController.view.window?.layer.addAnimation(transition, forKey: "kCATransition") sourceViewController.navigationController?.popViewControllerAnimated(false) } } } 

不需要创build一个自定义的segue,我把这个代码放在一起来呈现视图…

 UIViewController * nextView = [[UIStoryboard storyboardWithName:@“Main”bundle:nil] instantiateViewControllerWithIdentifier:@“YOUR_VIEW_CONTROLLER_STORYBOARD_NAME”];

 nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

 [self.navigationController presentViewController:nextViewanimation:YES完成:无];
 //(对于交叉解散,设置animation:YES。对于没有转换,设置animation:NO。)

希望这可以帮助任何遇到这个问题的人!

试试这个。

  let transition: CATransition = CATransition() transition.duration = 0.4 transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) transition.type = kCATransitionFade self.navigationController!.view.layer.addAnimation(transition, forKey: nil) let vc = self.storyboard?.instantiateViewControllerWithIdentifier("vcID") as! My_ViewController self.navigationController?.pushViewController(vc, animated: false) 

我的Swift版本可select检查offscourse!

  let storyboard = UIStoryboard.init(name: "Main", bundle: nil) if let stView = storyboard.instantiateViewControllerWithIdentifier("STVC") as? STVC { stView.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve self.navigationController?.presentViewController(stView, animated: true, completion: nil) } 

只要确保在IB中设置“视图控制器”的故事板ID即可

我所做的就是在Interface Builder中使用Storyboard,连接你需要移动的两个视图控制器(按住ctrl并拖动从原始视图控制器到目标视图控制器的点击),并且改变了segue属性。 我使用了以下内容:

在这里输入图像说明

然后我使用以下内容:

 [self performSegueWithIdentifier:@"showMovieDetailSegue" sender:self]; 

当你想删除它,只需从原始视图控制器调用这个:

 [self dismissViewControllerAnimated:YES completion:nil]; 

简单而快速。

Push / Pop UIVIewController FadeIn / FadeOut在Swift 3语法中,基于Eugene Braginets的回答

 class FadeInPushSegue: UIStoryboardSegue { var animated: Bool = true override func perform() { let sourceViewController = self.source let destinationViewController = self.destination let transition: CATransition = CATransition() transition.type = kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade sourceViewController.view.window?.layer.add(transition, forKey: "kCATransition") sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false) } } class FadeOutPopSegue: UIStoryboardSegue { override func perform() { let sourceViewController = self.source let transition: CATransition = CATransition() transition.duration = 0.4 transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) transition.type = kCATransitionFade; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade sourceViewController.view.window?.layer.add(transition, forKey: "kCATransition") _ = sourceViewController.navigationController?.popViewController(animated: false) } }