在通过导航控制器使用自定义动画过渡到SecondViewController后,它处于非活动状态

问题描述 投票:0回答:2

我有2个视图控制器(ViewController和SecondViewController)和1个导航控制器:enter image description here

我的项目使用从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)

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

ios swift animation uinavigationcontroller custom-transition
2个回答
0
投票

它可能是编辑器中的一个小故障尝试: - 重新启动Xcode - 清理项目 - 删除按钮及其参考,然后重新添加它们


0
投票

解。在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)
}
© www.soinside.com 2019 - 2024. All rights reserved.