从左到右调查动画

时间:2016-09-30 10:55:28

标签: ios xcode segue swift3 uistoryboardsegue

我试图实现一个自定义segue,它将为viewcontroller带来从右到左的动画效果。问题是,当我按下按钮以执行segue到所需的控制器时,我的应用程序崩溃。我收到以下错误:

  

无法创建类'(null)'

的segue

我是如何实现它的? 我创建了一个新类,并使用以下代码覆盖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,我使用自定义segue(由我的CustomTransition类生成的一个)绑定。当我运行应用程序并点击按钮时,我的应用程序崩溃,上面发布了错误。我不明白为什么会崩溃。我发现网上的每篇文章都与我实现它的方式类似。

2 个答案:

答案 0 :(得分:1)

你是否尝试过不使用segues而是为此目的进行过渡?

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

答案 1 :(得分:0)

您可以将该代码提取到Segue类并在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