我遇到了一种不寻常的行为,我被困住了一点,问题如下。
我正在使用BWWalkthrough库,以便将4张幻灯片作为启动屏幕。因此,在我的app委托中,我有下面的代码来初始化视图控制器:
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)
}
)
}
}现在的问题是,如果我在普通的视图控制器上使用相同的代码,按钮和动画就可以正常工作了。问题是当我使用上面从我的BWWalkthrough的最后一张幻灯片中定义的segue时。当我第一次点击按钮时,应该出现的视图控制器确实出现了,但没有相应的动画。关闭它并再次在按钮上录制之后,将播放动画,但返回一个错误:
不鼓励在分离的视图控制器上显示视图控制器。
如果我在标准动画中使用该按钮(不使用我的自定义动画代码),则不会出现错误,并且会播放默认的动画。
我似乎找不到解决这个问题的办法。有人偶然发现这样的东西吗?
发布于 2016-10-09 05:39:02
这里的问题在于BWWalkthrough库,它使用滚动视图来表示所添加的各种ViewControllers的所有视图。
因此,在滚动视图的开头添加dst.view (偏移量屏幕宽度为0),然后将其转换为偏移量(0,0)。
所有这些都是屏幕外的,因为您目前在演练的第三个屏幕上(在偏移量(屏幕宽度*3,0)处)。因此,当segue结束时,您不会看到动画,也不会直接看到呈现的视图控制器。
要解决这一问题,请将dst.view添加到滚动视图的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是否为滚动视图,如果是,则将dst.view添加到滚动视图的超级视图中。
https://stackoverflow.com/questions/39796010
复制相似问题