我正在使用BWWalkthrough库,以便将4张幻灯片作为启动画面.所以在我的appdelegate中,我有以下代码初始化viewcontrollers:
let storyboard = UIStoryboard(name: "SlidesFlow",bundle: nil) let walkthrough = storyboard.instantiateViewController(withIdentifier: "SlidesView") as! BWWalkthroughViewController let page_zero = storyboard.instantiateViewController(withIdentifier: "page_1") let page_one = storyboard.instantiateViewController(withIdentifier: "page_2") let page_two = storyboard.instantiateViewController(withIdentifier: "page_3") let page_three = storyboard.instantiateViewController(withIdentifier: "page_4") walkthrough.delegate = self walkthrough.addViewController(page_zero) walkthrough.addViewController(page_one) walkthrough.addViewController(page_two) walkthrough.addViewController(page_three)
一切都按预期工作,所以这里没问题.在viewController page_three上我有一个按钮,它使用自定义segue动画将我重定向到另一个视图控制器
class sentSegueFromRight: UIStoryboardSegue { override func perform() { let src = self.source as UIViewController let dst = self.destination as UIViewController 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) } ) } }
现在的问题是,如果我在普通的viewcontroller上使用相同的代码按钮和动画工作没有问题.问题是当我在BWWalkthrough的最后一张幻灯片中使用上面定义的segue时.第一次使用按钮时,应该出现的viewcontroller会出现但没有相应的动画.关闭它并再次点击按钮后播放动画但返回错误:
Presenting view controllers on detached view controllers is
discouraged
解决方法
因此,您可以在scrollview的开头添加dst.view(在offset screenwidth,0处),然后将其转换为offset(0,0).
所有这些都是屏幕外的,因为您目前处于演练的第三个屏幕(偏移(屏幕宽度* 3,0)).因此,当segue结束时,您无法看到动画并直接看到呈现的视图控制器.
要解决此问题,请将segue中的dst.view添加到scrollview的superview中.即代替src.view.superview?.insertSubview(dst.view,aboveSubview:src.view)
在segue中写src.view.superview?.superview?.insertSubview(dst.view,aboveSubview:src.view). (假设您仅使用了演练中的segue)
如果你打算在其他地方使用segue,那么你可以在segue中添加一个类型检查来检查src.view的superview是否是一个scrollview,如果是,则将dst.view添加到scrollview的superview.