通过导航控制器通过自定义动画过渡到SecondViewController中的非活动视图后

时间:2019-04-15 06:12:06

标签: ios swift animation uinavigationcontroller custom-transition

我有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。我的代码中的问题出在哪里?我想按一下按钮来工作。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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)
}