是否可以在Storyboard中的视图控制器之间进行淡入和淡出过渡。或者没有过渡。
如果有可能,它的代码是什么?
答案 0 :(得分:138)
如果展示模态视图控制器,您可以指定modalTransitionStyle
UIModalTransitionStyleCrossDissolve
。如果在故事板中使用segue执行此操作,请选择segue的属性检查器,并在那里指定过渡样式:
如果以编程方式呈现视图控制器,您可以在故事板中的视图控制器之间使用“Cross Dissolve”的“Transition”定义模态segue,然后让源视图控制器执行此segue:
[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 2013视频Custom Transitions Using View Controllers。
但是,例如,如果要将iOS 7中的推送和弹出动画自定义为淡入淡出,则应为导航控制器指定delegate
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.delegate = self;
}
然后,您将实现animationControllerForOperation
指定动画对象以进行推送和弹出:
- (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;
}
您显然必须实现自定义推送和弹出动画制作工具,例如:
@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
,除非你有其他微妙的定制你想雇用)。有关详细信息,请参阅上述Custom Transitions Using View Controllers。
底线,iOS 7的自定义过渡是一种稍微复杂但非常强大的方式,可以对过渡动画提供巨大的控制。
答案 1 :(得分:16)
用于创建自定义分段创建UIStoryboard segue的子类。 例如:
// 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中选择segue并设置Style:Custom和Class:MCFadeSegue。
答案 2 :(得分:8)
在Swift中推送/弹出UIVIewController 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)
}
}
}
答案 3 :(得分:6)
无需创建自定义segue,我将此代码放在一起以呈现视图...
UIViewController *nextView = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"YOUR_VIEW_CONTROLLER_STORYBOARD_NAME"]; nextView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self.navigationController presentViewController:nextView animated:YES completion:nil]; // (For a cross dissolve, set animated:YES. For no transition, set animated:NO.)
希望这有助于任何遇到此问题的人!
答案 4 :(得分:5)
试试这个。
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)
答案 5 :(得分:4)
我的Swift版本,带有可选的检查功能!
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)
}
只需确保设置&#34; View控制器&#34;的故事板ID。在IB
答案 6 :(得分:2)
我所做的是在Interface Builder中使用Storyboard,连接你需要移动到/从和移回的两个视图控制器(按住ctrl并从原点视图控制器拖动单击到目标视图控制器),并更改了segue属性。我使用了以下内容:
然后我使用了以下内容:
[self performSegueWithIdentifier:@"showMovieDetailSegue" sender:self];
如果要删除它,只需从origin viewcontroller中调用它:
[self dismissViewControllerAnimated:YES completion:nil];
简单快捷。
答案 7 :(得分:1)
根据Eugene Braginets&#39; s answer
以 Swift 3 语法推送/弹出UIVIewController FadeIn / FadeOutclass 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)
}
}
答案 8 :(得分:1)
这使我疯了几个小时。这些解决方案似乎都不是我想要的,而且我已经失去了理智。最终提出了以下内容,它使用淡入/淡入过渡在单个NSWindow中交换视图控制器。首先,它执行淡出操作,然后在完成处理程序中,将窗口的视图设置为新视图并执行淡入操作。也许有更好的方法可以做到这一点,但这是我可以共同破解的最简单方法
class CustomSegue : NSStoryboardSegue {
override func perform() {
guard let fromController = sourceController as? NSViewController else {
return;
}
guard let toController = destinationController as? NSViewController else {
return;
}
let animationDuration = 0.5;
NSAnimationContext.runAnimationGroup({
$0.duration = animationDuration;
fromController.view.animator().alphaValue = 0.0;
}, completionHandler: {
fromController.view.window?.contentViewController = toController;
toController.view.alphaValue = 0.0;
NSAnimationContext.runAnimationGroup({
$0.duration = animationDuration;
toController.view.animator().alphaValue = 1.0;
})
});
}
}
Apple文档中缺少示例,这使我想动脑筋,Objective C和Swift的混合使用只是……ba!
答案 9 :(得分:1)
快捷键5:
花了数小时寻找合适的解决方案Rob's之后,一个人满足了我的所有需求,经过一些调整,它是完美的。
创建淡入淡出的View Controller类:
class FadeViewControllerTransition: NSObject, UIViewControllerAnimatedTransitioning {
var fadeOut: Bool = false
func transitionDuration(
using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
if fadeOut {
animateFadeOut(using: transitionContext)
return
}
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
transitionContext.containerView.addSubview(toViewController.view)
toViewController.view.alpha = 0.0
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
toViewController.view.alpha = 1.0
}, completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
func animateFadeOut(using transitionContext: UIViewControllerContextTransitioning) {
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
transitionContext.containerView.insertSubview(toViewController.view, belowSubview: fromViewController.view)
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
fromViewController.view.alpha = 0.0
}, completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}
}
在导航控制器的第一个View Controller中:
class MainViewController: UIViewController, UIViewControllerTransitioningDelegate, UINavigationControllerDelegate {
private let fadeVCTransition = FadeViewControllerTransition()
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
fadeVCTransition.fadeOut = (operation == .pop)
return fadeVCTransition
}
就是这样。
答案 10 :(得分:0)
要显示而没有过渡: