为什么自定义交叉溶解序列中的视图控制器的主视图没有对齐?

时间:2019-04-13 00:00:40

标签: ios swift uiviewcontroller uinavigationcontroller uistoryboardsegue

我编写了一个自定义UIStoryboardSegue子类,以在两个UIViewController之间创建淡入/淡入,交叉溶解过渡。

视图控制器的主视图具有位于每个按钮中心的按钮(分别标记为“ View Controller 1”和“ View Controller 2”),因此我假设当我交叉溶解一个按钮时,将它们都设置为具有相同超级视图的子级,并使它们的框架相同-按钮也将对齐。

class SwapSegue: UIStoryboardSegue {

    override func perform() {
        fade()
    }

    func fade() {
        destination.view.alpha = 0

        source.view.superview?.addSubview(destination.view)
        destination.view.frame = source.view.frame

        UIView.animate(withDuration: 1, delay: 0, options: .curveEaseInOut, animations: {
            self.destination.view.alpha = 1
        }, completion: { success in
            if var controllerStack = self.source.navigationController?.viewControllers {
                controllerStack[controllerStack.firstIndex(of: self.source)!] = self.destination
                self.source.navigationController?.setViewControllers(controllerStack, animated: false)
            } else {
                self.source.present(self.destination, animated: false, completion: nil)
            }
        })

    }
}

这几乎可行,但是在过渡期间无法正确定位目标视图,因此当segue的动画完成时,最后会有一个“ pop”。对齐方式似乎偏离了导航栏的高度,所以我怀疑这是一个线索。

Swap pop

(注意:在此循环GIF中,您可以看到在“ View Controller 1”上按下了按钮,动画的淡入淡出与按钮未对齐,然后在动画完成和目标视图结束时出现了“ pop”控制器终于正确放置。)

我如何正确放置它,以便它应该对齐,动画是否流畅?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。我不确定100%为何可行,因此,如果可以解释,请在下面的评论中进行。

最初,我通过自动布局使两个按钮位于视图控制器主视图的中央。

通过向每个视图控制器的主视图添加一个子视图(我们称其为“内容视图”),固定到其边缘,并将每个按钮居于该视图的中心,该过渡现在按预期工作。

Intermediate "content view"

enter image description here