我正在尝试为多个 View Controller 之间的转场创建自定义动画,以便新用户逐步设置他们的应用程序。我尝试对所有转场使用相同的自定义 UIStoryBoardSegue 类(我在其中重写执行函数),因为当它们都是相同的动画时,为每个转场创建一个似乎效率很低。
以下内容第一次有效,但当我稍后再次使用相同的 UIStoryBoardSegue 类时,甚至没有到达断点。
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)
})
}
知道缺少什么,或者有更好的方法来实现我想要做的事情吗?谢谢。
最佳答案
我来回答你的问题吧。在动画结束时,您忘记了源 Controller 和目标 Controller 之间建立的关系。换句话说,您必须手动显示您的目的地。
如果您不想从 vc 堆栈中删除源 View Controller ,请不要关闭源 View Controller 并添加 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。希望这能解决您的问题。
关于ios - 无法为多个 Segue 重用自定义 StoryboardSegue 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52463610/