[英]Segue with animation from left to right
I'm trying to implement a custom segue which will bring the viewcontroller with animation from right to left.我正在尝试实现一个自定义的 segue,它将从右到左为视图控制器带来动画。 The problem is that my app crash when I press the button in order to perform the segue to the needed controller.
问题是当我按下按钮以对所需控制器执行 segue 时,我的应用程序崩溃。 I receive the following error:
我收到以下错误:
Could not create a segue of class '(null)'
无法创建类 '(null)' 的 segue
How did I implement it?我是如何实施的? I created a new class and override the perform function with the following code:
我创建了一个新类并使用以下代码覆盖执行函数:
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)
}
)
}
} }
In my storyboard I have an UIButtoon which I tied using a custom segue ( the one generated by my CustomTransition class ).在我的故事板中,我有一个 UIButton,我使用自定义转场(由我的 CustomTransition 类生成的转场)绑定了它。 When I run the app and tap on the button, my app crash with the error posted above.
当我运行应用程序并点击按钮时,我的应用程序崩溃并显示上面发布的错误。 I can't understand why is it crashing.
我不明白为什么它会崩溃。 Every article on the web that I found is similar to the way i implemented it.
我在网上找到的每篇文章都与我实现它的方式相似。
Did you try not to use segues but transitions for this purpose?为此,您是否尝试不使用转场而是使用过渡?
// 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)
You can extract that code to a Segue class and use on Storyboard:您可以将该代码提取到 Segue 类并在 Storyboard 上使用:
LeftToRightSegue.h: LeftToRightSegue.h:
@interface LeftToRightSegue : UIStoryboardSegue
@end
LeftToRightSegue.m从左到右Segue.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.