我已经实现了一个导航控制器,以便结合旋转盘类型的布局(将每个VC布局成一个整体旋转的圆形视图)。控制器被配置为使用自定义的过渡类,如下图所示: -Swift 3中的自定义转换不能正确转换
import UIKit class RotaryTransition: NSObject, UIViewControllerAnimatedTransitioning { let isPresenting :Bool let duration :TimeInterval = 0.5 let animationDuration: TimeInterval = 0.7 let delay: TimeInterval = 0 let damping: CGFloat = 1.4 let spring: CGFloat = 6.0 init(isPresenting: Bool) { self.isPresenting = isPresenting super.init() } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { //Get references to the view hierarchy let fromViewController: UIViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)! let toViewController: UIViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)! let sourceRect: CGRect = transitionContext.initialFrame(for: fromViewController) let containerView: UIView = transitionContext.containerView if self.isPresenting { // Push //1. Settings for the fromVC ............................ // fromViewController.view.frame = sourceRect fromViewController.view.layer.anchorPoint = CGPoint(x: 0.5, y: 3); fromViewController.view.layer.position = CGPoint(x: fromViewController.view.frame.size.width/2, y: fromViewController.view.frame.size.height * 3); //2. Setup toVC view........................... containerView.insertSubview(toViewController.view, belowSubview:fromViewController.view) toViewController.view.layer.anchorPoint = CGPoint(x: 0.5, y: 3); toViewController.view.layer.position = CGPoint(x: toViewController.view.frame.size.width/2, y: toViewController.view.frame.size.height * 3); toViewController.view.transform = CGAffineTransform(rotationAngle: 15 * CGFloat(M_PI/180)); //3. Perform the animation............................... UIView.animate(withDuration: animationDuration, delay:delay, usingSpringWithDamping: damping, initialSpringVelocity: spring, options: [], animations: { fromViewController.view.transform = CGAffineTransform(rotationAngle: -15 * CGFloat(M_PI/180)); toViewController.view.transform = CGAffineTransform(rotationAngle: 0); }, completion: { (animated: Bool) ->() in transitionContext.completeTransition(true) }) } else { // Pop //1. Settings for the fromVC ............................ fromViewController.view.frame = sourceRect fromViewController.view.layer.anchorPoint = CGPoint(x: 0.5, y: 3); fromViewController.view.layer.position = CGPoint(x: fromViewController.view.frame.size.width/2, y: fromViewController.view.frame.size.height * 3); //2. Setup toVC view........................... // toViewController.view.frame = transitionContext.finalFrame(for: toViewController) toViewController.view.layer.anchorPoint = CGPoint(x: 0.5, y: 3); toViewController.view.layer.position = CGPoint(x: toViewController.view.frame.size.width/2, y: toViewController.view.frame.size.height * 3); toViewController.view.transform = CGAffineTransform(rotationAngle: -15 * CGFloat(M_PI/180)); containerView.insertSubview(toViewController.view, belowSubview:fromViewController.view) //3. Perform the animation............................... UIView.animate(withDuration: animationDuration, delay:delay, usingSpringWithDamping: damping, initialSpringVelocity: spring, options: [], animations: { fromViewController.view.transform = CGAffineTransform(rotationAngle: 15 * CGFloat(M_PI/180)); toViewController.view.transform = CGAffineTransform(rotationAngle: 0); }, completion: { //When the animation is completed call completeTransition (animated: Bool) ->() in transitionContext.completeTransition(true) }) } } func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return duration; } }
的意见如何运动的表示是显示在下面......两个红色区域是问题,后面会解释插图
。呈现(推)翻译工作正常 - 2移动到1和3移动到2.然而,解雇(流行)翻译没有,从而解雇VC看起来正确地移动(2移动到3),但呈现(前一个)VC到达或者在错误的地方或者尺寸大小不正确的地方...
与类一样,翻译导致2移动到3(正确),但是1移动到4该视图的尺寸正确,但似乎偏离了预期位置的看似任意的距离。我从此尝试了各种不同的解决方案。
在弹出部我试图将下面的行(在代码注释): -
toViewController.view.frame = transitionContext.finalFrame(for: toViewController)
...,但VC现在最终被收缩(1个移动至5)。我希望有人能看到我正在犯的可能的愚蠢错误。我试着简单地将push部分复制到pop部分(并反转所有内容),但它不起作用!
仅供参考......那些需要知道如何来装过渡到一个UINavigationController - 在UINavigationControllerDelegate添加到您的导航控制器,具有以下功能一起...
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { let transition: SwingTransition = SwingTransition.init(isPresenting: (operation == .push ? true : false)) return transition; }
下图显示了所有意见将共享相同的始发点(用于翻译)。目标是给出一个左轮手枪桶将每个VC移动到视野中的错觉。顶部中央视图表示查看窗口,显示堆栈中的第三个视图。道歉对穷人的视觉效果......
代码复习_and_问题解决,不错的工作! – jrturton
这看起来像我期待的答案,它非常接近......不幸的是,过渡似乎来自不同的位置,破坏了视图固定在旋转光盘上的错觉。我放慢了动画速度,一些奇怪的原因,向左移动的视图来自视图右侧的某个位置,而右侧的视图来自左侧的某个位置。我试过在半宽处添加,但它们似乎没有效果 – NickSaintJohn
想象一下,视图是左轮手枪中的子弹,而枪管就是视窗。他们应该从相同的翻译来源旋转。我仍然试图调整,但没有得到任何地方 – NickSaintJohn