I have 2 view controllers (ViewController and SecondViewController) and 1 navigation controller:enter image description here

My project using custom transition animation from ViewController to 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
    }
}

In ViewController (where door image) I use а delay to start the transition animation and go to the SecondViewController (where green background).

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 has Button press function:

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

Animation works correctly, but SecondViewController (green background) does not respond to button presses. Code in SecondViewController

print(view.isUserInteractionEnabled)

return true. Where is the problem in my code? I want to press the button to work.

2

There are 2 best solutions below

7
Bajesh On

It may be a glitch in the editor Try: - Restarting Xcode - Cleaning the Project - Deleting the Button and it's reference and then re-adding them

0
Tkas On

Solution. Remove this in TransitionManager:

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

And add this:

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