首页 > 解决方案 > 通过导航控制器使用自定义动画转换到 SecondViewController 后,SecondViewController 中的非活动视图

问题描述

我有 2 个视图控制器(ViewController 和 SecondViewController)和 1 个导航控制器:在此处输入图像描述

我的项目使用从 ViewController 到 SecondViewController 的自定义过渡动画:

import UIKit

class TransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.8
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        var transform = CATransform3DIdentity
        let container = transitionContext.containerView
        guard let fromView = transitionContext.view(forKey: .from) else { return }
        guard let toView = transitionContext.view(forKey: .to) else { return }

        transform.m34 = -0.0019
        container.layer.sublayerTransform = transform
        container.insertSubview(toView, belowSubview: fromView)
        fromView.layer.anchorPoint = CGPoint(x: 0.0, y: 0.5)
        fromView.layer.position    = CGPoint(x: 0, y: UIScreen.main.bounds.midY)

        UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
            fromView.layer.transform = CATransform3DMakeRotation(-.pi/2, 0, 1.0, 0)
        }
    }

    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
}

在 ViewController(其中门图像)中,我使用延迟来启动过渡动画并转到 SecondViewController(其中绿色背景)。

class ViewController: UIViewController {
    let transitionManager = TransitionManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: {
            self.performSegue(withIdentifier: "segue", sender: self)
        })
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard let navigationViewController = segue.destination as? UINavigationController else { return }

        navigationViewController.transitioningDelegate = transitionManager
    }
}

SecondViewController 具有按钮按下功能:

class SecondViewController: UIViewController, UIViewControllerTransitioningDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        print(view.isUserInteractionEnabled)
    }

    @IBAction func tapBtn(_ sender: UIButton) {
        print("btn pressed") //it is not worked. Why?
    }
}

动画正常工作,但 SecondViewController(绿色背景)不响应按钮按下。SecondViewController 中的代码

print(view.isUserInteractionEnabled)

返回真。我的代码中的问题在哪里?我想按下按钮来工作。

标签: iosswiftanimationuinavigationcontrollercustom-transition

解决方案


解决方案。在 TransitionManager 中删除它:

UIView.animate(withDuration: transitionDuration(using: transitionContext)) {
   fromView.layer.transform = CATransform3DMakeRotation(-.pi/2, 0, 1.0, 0)
}

并添加:

UIView.animate(withDuration:  transitionDuration(using: transitionContext), delay: 0.0, options: .curveEaseInOut, animations: {
    fromView.layer.transform = CATransform3DMakeRotation(-.pi/2, 0, 1.0, 0)
}) { (finished) in
    transitionContext.completeTransition(true)
}

推荐阅读