0

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.

Tkas
  • 302
  • 3
  • 14

2 Answers2

0

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

Bajesh
  • 54
  • 3
  • is button.isEnabled set to true (either in code or in the settings to the right)? – Bajesh Apr 15 '19 at 06:31
  • Yes, it is enabled in code (btn.isEnabled = true) and in Main.storyboard settings – Tkas Apr 15 '19 at 06:33
  • Try deleting DispacheQueue.main.asyncAfter() and running self.performSegue directly. – Bajesh Apr 15 '19 at 06:51
  • or use something like "perform(#selector(yourseguefunction), with: nil, afterDelay: 0.3)" (yourseguefunction is the function thats segues) – Bajesh Apr 15 '19 at 06:51
  • I am delete DispacheQueue.main.asyncAfter() but self.performSegue is not working in viewDidLoad! I do not know why... I am add a button to ViewController and put self.performSegue code in button action. Animation is work correctly, but Button action in SecondViewController still not working... – Tkas Apr 15 '19 at 06:56
  • And I am try use "perform(#selector(yourseguefunction), with: nil, afterDelay: 0.3)" but Button action in SecondViewController still not working – Tkas Apr 15 '19 at 07:04
  • I am add solution of this question – Tkas Apr 15 '19 at 08:54
0

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)
}
Tkas
  • 302
  • 3
  • 14