ios - 为什么主视图没有在自定义交叉溶解转场中与 View Controller 对齐?

标签 ios swift uiviewcontroller uinavigationcontroller uistoryboardsegue

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

View Controller 的主视图有位于每个 View 中心的按钮(标记为“View Controller 1”和“View Controller 2”,因此我假设当我将一个 View 与另一个 View 交叉融合时——将它们都设置为相同父 View 的 subview 并使它们的框架相同——按钮也将对齐。

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)
            }
        })

    }
}

这几乎可以工作,但是它在过渡期间没有正确定位目标 View ,所以当 segue 的动画完成时,最后会有一个“弹出”。对齐方式似乎偏离了导航栏的大致高度,所以我怀疑这是一个线索。

Swap pop

(注意:在这个循环 GIF 中,您可以看到“View Controller 1”上的按钮按下,按钮错位的动画交叉淡入淡出,然后是动画完成时最后的“弹出”和目标 View Controller 终于正确放置。)

如何正确定位它,使其按应有的方式对齐并且动画流畅?

最佳答案

我找到了一个解决方案。我不是 100% 确定为什么会这样,所以如果你能解释一下,请在下面的评论中解释。

最初,我通过自动布局将两个按钮置于 View Controller 主视图的中央。

通过向每个 View Controller 的主视图添加一个 subview (让我们称之为“内容 View ”),固定到它的边缘,并在那个 View 中将每个按钮居中,过渡现在可以按预期工作.

Intermediate "content view"

enter image description here

关于ios - 为什么主视图没有在自定义交叉溶解转场中与 View Controller 对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55660803/

相关文章:

swift - Swift 3 中 NSFetchRequests 的集合

iOS:如何在弹出 UIViewController 的子 UIViewController 后将其返回 "Refresh"?

ios - -[__ NSArrayI objectForKeyedSubscript:]:无法识别的选择器已发送到实例IN Xcode6 Objective-C iOS

ios - 碰撞处理在 iOS 13 中不起作用

ios - 为什么调用 super 是此方法的最后一件事而不是第一件事?

Swift 命令行工具运行时崩溃 dyld : Symbol not found: __pthread_atfork_child

ios - 您的帐户已在 Xcode 7.3 中拥有有效的 iOS 开发证书

ios - 在 swift 3 playground 中阅读 plist

ios - 普通 View Controller 内的 TableView

iphone - UIScrollView 和 Cocos2D