ios - 自定义 UINavigationController 动画

标签 ios swift uiviewanimationtransition ios-animations

我正在尝试实现自定义过渡动画,这就是我所拥有的

class NavDelegate: NSObject, UINavigationControllerDelegate {
    private let animator = Animator()

    func navigationController(_ navigationController: UINavigationController,
                              animationControllerFor operation: UINavigationControllerOperation,
                              from fromVC: UIViewController,
                              to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return animator
    }
}

class Animator: NSObject, UIViewControllerAnimatedTransitioning {
    func transitionDuration(using context: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 10.0
    }

    func animateTransition(using context: UIViewControllerContextTransitioning) {
        let fromViewController = context.viewController(forKey: UITransitionContextViewControllerKey.from)!
        let toViewController = context.viewController(forKey: UITransitionContextViewControllerKey.to)!
        if fromViewController is ViewController {
            self.pushAnimation(from: fromViewController as! ViewController,
                               to: toViewController as! VC2ViewController,
                               with: context)
        }

        if toViewController is ViewController {
            print("pop")
        }

    }

    func pushAnimation(from viewController: ViewController,
                       to destinationViewController: VC2ViewController,
                       with context: UIViewControllerContextTransitioning) {

        context.containerView.addSubview(destinationViewController.view)


        //block 1
        for cell in viewController.tableView1.visibleCells {
            if let castCell = cell as? VC1TableViewCell {
                castCell.contentViewToLeft.constant = -UIScreen.main.bounds.width
                castCell.contentViewToRight.constant = UIScreen.main.bounds.width
                let duration = Double(viewController.tableView1.visibleCells.index(of: cell)!)/Double(viewController.tableView1.visibleCells.count) + 0.2
                UIView.animate(withDuration: duration, animations: {
                    castCell.layoutIfNeeded()
                }) { animated in
                }
            }
        }

        //block 2
        for cell in destinationViewController.tableView2.visibleCells {
            if let castCell = cell as? VC2TableViewCell {
                castCell.contentViewToLeft.constant = -UIScreen.main.bounds.width
                castCell.contentViewToRight.constant = UIScreen.main.bounds.width
                let duration = Double(destinationViewController.tableView2.visibleCells.index(of: cell)!)/Double(destinationViewController.tableView2.visibleCells.count) + 0.2
                UIView.animate(withDuration: duration, animations: {
                    castCell.layoutIfNeeded()
                }) { animated in
                    if duration > 1.1 {
                        context.completeTransition(!context.transitionWasCancelled)
                    }
                }
            }
        }
    }
}

问题是目标 Controller ( block 2)的约束动画从来没有动画,layoutIfNeeded() 在没有动画的情况下执行,尽管 block 1 正在工作。可能是什么问题?

最佳答案

我花了几天时间测试

transitionDuration(使用上下文:UIViewControllerContextTransitioning?)

函数,发现都是在DispatchQueue.main.async{}中调用一些toViewController动画 block 。不确定它是如何工作的,但我已经让我的代码按照我的计划工作。

关于ios - 自定义 UINavigationController 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45546958/

相关文章:

ios - Swift 4 Codable 从字符串解码 URL

ios - 为什么在进行本地搜索时会收到 MKErrorDomain 错误?

ios - 使用 UIViewAnimationTransitionFlipFromRight 在按下时翻转 UIButton 并在 "the other side"上显示新按钮

ios - 如何在不将views设置为零的情况下进行水平翻转动画?

ios - 在swift中使用struct创建数组

ios - 使用共享 uiimage 在 View Controller 之间转换

ios - 如何从 Gmail 应用程序在 Safari 中打开链接?

ios - Swift - 选择不同声音时不播放不同声音

ios - 使用 Storyboard 重新定位 textview

ios - 如何在 Swift 中使用#selector 操作传递参数?