不能为多个 segues 重用自定义 StoryboardSegue class

Cannot reuse custom StoryboardSegue class for multiple segues

我正在尝试为多个视图控制器之间的转场创建自定义动画,以便新用户逐步设置他们的应用程序。我正在尝试对所有 segues 使用相同的自定义 UIStoryBoardSegue class(我在其中重写 perform func),因为当它们都是相同的动画时为每个创建一个似乎效率低下。

以下是第一次运行,但当我稍后再次使用相同的 UIStoryBoardSegue class 时,甚至没有到达断点。

override func perform() {
    let firstVCView = source.view!
    let secondVCView = destination.view!

    let screenWidth = UIScreen.main.bounds.size.width

    secondVCView.frame = secondVCView.frame.offsetBy(dx: screenWidth, dy: 0.0)

    let superView = firstVCView.superview
    superView?.insertSubview(secondVCView, aboveSubview: firstVCView)

    UIView.animate(withDuration: 0.35, animations: { () -> Void in
        firstVCView.frame = firstVCView.frame.offsetBy(dx: -screenWidth, dy: 0.0)
        secondVCView.frame = secondVCView.frame.offsetBy(dx: -screenWidth, dy: 0.0)
    }, completion: { (finished) -> Void in
        self.source.dismiss(animated: false, completion: nil)
    })
}

知道缺少什么吗,或者是否有更好的方法来实现我想要做的事情?谢谢

让我来回答你的问题。在动画结束时,您忘记了在源控制器和目标控制器之间建立关系。换句话说,您必须手动显示您的目的地。

如果您不想从 vc 堆栈中删除您的源视图控制器,请不要关闭您的源视图控制器并添加 self.source.present(self.destination, animated : false, completion: nil), 像下面这样。

 UIView.animate(withDuration: 0.35, animations: { () -> Void in
        firstVCView.frame = firstVCView.frame.offsetBy(dx: -screenWidth, dy: 0.0)
        secondVCView.frame = secondVCView.frame.offsetBy(dx: -screenWidth, dy: 0.0)
    }, completion: { (finished) -> Void in
           self.source.present(self.destination, animated: false, completion: nil)
      //  self.source.dismiss(animated: false, completion: nil)

    })

如果您想关闭,您必须使用源 vc 的父级 vc 来推送或显示目标 vc,例如 UINavigationViewContoller 或 UITabViewController。希望这能解决您的问题。