2016-09-30 87 views
2

我想实现一个自定义的塞格,它将从右向左带动视图控制器。问题是我的应用程序崩溃时,我按下按钮,以执行segue到所需的控制器。我收到以下错误:与动画从左到右的塞格

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

我是如何实现它的? 我创建了一个新的类并覆盖用下面的代码的执行功能:

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类生成的一个)。当我运行该应用程序并点击按钮时,我的应用程序崩溃与上面发布的错误。我不明白为什么它会崩溃。我发现网上的每篇文章都与我实施它的方式类似。

回答

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) 
+0

什么是navigationController?你的sourceVC必须是UINavigationController吗?如果源和目标只是两个普通的UIViewControllers,就像原始问题一样? – Cindeselia

+0

@Cindeselia你可以从属性'sourceVC.navigationController'访问导航控制器。 – Astoria

0

您可提取代码的Segue公司类和故事板使用:

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