动画从左到右继续

Segue with animation from left to right

我正在尝试实现自定义转场,它将 viewcontroller 与动画从右向左显示。问题是当我按下按钮以执行到所需控制器的 segue 时,我的应用程序崩溃了。我收到以下错误:

Could not create a segue of class '(null)'

我是如何实施的? 我创建了一个新的 class 并使用以下代码覆盖了 perform 函数:

import UIKit

class CustomTransition: UIStoryboardSegue {

override func perform()
{
    let src = self.source
    let dst = self.destination

    src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
    dst.view.transform = CGAffineTransform(translationX: -src.view.frame.size.width, y: 0)

    UIView.animate(withDuration: 0.25,
                               delay: 0.0,
                               options: UIViewAnimationOptions.curveEaseInOut,
                               animations: {
                                dst.view.transform = CGAffineTransform(translationX: 0, y: 0)
        },
                               completion: { finished in
                                src.present(dst, animated: false, completion: nil)
        }
    )
}

}

在我的故事板中,我有一个 UIButtoon,我使用自定义转场(由我的 CustomTransition class 生成的转场)将其绑定。当我 运行 应用程序并点击按钮时,我的应用程序崩溃并出现上面发布的错误。我不明白为什么它会崩溃。我在网上找到的每篇文章都与我实现它的方式相似。

您是否尝试过不使用转场而是为此目的使用过渡?

// create transition
let transition = CATransition()
transition.duration = 0.3
// check other types
transition.type = kCATransitionMoveIn
// move from left
transition.subtype = kCATransitionFromLeft
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
navigationController.view.layer.addAnimation(transition, forKey: kCATransition)
// push controller
navigationController.pushViewController(controller, animated: false)

您可以将该代码提取到 Segue class 并在 Storyboard 上使用:

LeftToRightSegue.h:

@interface LeftToRightSegue : UIStoryboardSegue

@end

LeftToRightSegue.m

@implementation LeftToRightSegue
-(void)perform
{
    CATransition * transition = [CATransition new];
    transition.duration = 1;
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromLeft;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.sourceViewController.navigationController.view.layer addAnimation:transition forKey:kCATransition];
    [self.sourceViewController.navigationController pushViewController:self.destinationViewController animated:false];
}
@end