UIViewControllerTransitioningDelegate on tvOS not being called - swift

So I'm trying to do an animated transition between viewcontrollers on tvOS 10.
The UIViewControllerTransitioningDelegate protocol is available on tvOS so I assumed I could animate it as well. But for some reason none of the functions are ever called when presenting the new viewcontroller.
I have no idea what I'm doing wrong since I'm doing basically the exact same on iOS.
Here is the code I'm using:
Presenting code
func showStuff() {
let viewController = ResultViewController()
viewController.transitioningDelegate = ResultViewControllerTransitionManager()
viewController.modalPresentationStyle = .custom
navigationController?.present(viewController, animated: true, completion: nil)
}
Transition Delegate
class ResultViewControllerTransitionManager: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
var duration = 0.5
var isPresenting = false
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {return}
guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {return}
(...)
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresenting = false
return self
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
isPresenting = true
return self
}
}

You can implement UIViewControllerAnimatedTransitioning and UIViewControllerTransitioningDelegate protocols inside your first view controller. Your first UIViewController may have the following code:
class ViewController: UIViewController, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
func showStuff() {
let viewController = ResultViewController()
viewController.transitioningDelegate = self
viewController.modalPresentationStyle = .custom
self.present(viewController, animated: true, completion: nil)
}
var duration = 0.5
var isPresenting = false
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 2.0
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {return}
guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {return}
}
}
Also, I present a new controller from the first one (not from the navigationController).

So apparently initialising the animationmanager in the showStuff() function was the problem. Storing it as a property in the Main viewcontroller and passing it along did work.

Wouldn't the issue be that you are using a navigationController, so that navigationController?.delegate needs to equal your ResultViewControllerTransitionManager()?
We have one at work using a push animation (should be the same issue), but we set
navigationController?.delegate = transitionDelegate

Related

Get wrong fromController using custom transition

I did a custom transition between ViewControllerA and ViewControllerB. But the fromVc type is not ViewControllerA. (The Hierarchy is: Tab -> VC_D -> (push)VC_C -> (present)VC_A -> (present)VC_B)
I want the fromVC to be ViewControllerA, or I can get ViewControllerA somehow. (I also use transitionContext.viewController(forKey: .from)?.children.last or first, still not working)
In ViewControllerA:
extension ViewControllerA: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if presented is ViewControllerB {
return Animator()
}
return nil
}
// here is how I present ViewControllerB in ViewControllerA's viewModel
let vcB = ViewControllerB()
vcB.transitioningDelegate = delegate
vcB.modalPresentationStyle = .custom
self.delegate.present(vcB, animated: true, completion: nil)
in ViewControllerB: nothing special
in Aminator
public class Animator: NSObject, UIViewControllerAnimatedTransitioning {
public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 3
}
public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// I need to do some staff in ViewControllerA so that I need to get fromVc.
guard
fromVc = transitionContext.viewController(forKey: .from)? as ViewControllerA,
toVc = transtionContext.viewController(forKEy: .to)? as ViewControllerB else {return}
// do some animation in CATransaction and settransitionContext.completeTransition(true)
// ....
}
}

Swift constraints issue when using custom segue

I am using custom segue to present a view controller, it works fine when I don't use any constraint, once I add a constraint to the label on the destination view controller, then it pops up an error saying:
"negative sizes are not supported in the flow layout"
This is my custom segue code
class PresentTimeMachineSegue: UIStoryboardSegue {
override func perform() {
destination.transitioningDelegate = self
destination.modalPresentationStyle = .fullScreen
source.present(destination, animated: true, completion: nil)
}
}
extension PresentTimeMachineSegue: UIViewControllerTransitioningDelegate {
public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return TimeMachineViewPresentAnimationController()
}
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return TimeMachineViewPresentAnimationController()
}
}
class TimeMachineViewPresentAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
CalendarViewController
let containerView = transitionContext.containerView
let toViewController = transitionContext.viewController(forKey: .to) as! TimeMachineViewController
print("transition")
if toViewController.calendarView != nil {
//toViewController.view.addSubview(toViewController.calendarView!)
containerView.addSubview(toViewController.view)
toViewController.inlitializeUI()
}
toViewController.view.backgroundColor = UIColor.white.withAlphaComponent(0)
UIView.animate(withDuration: 0.8, animations: {
toViewController.view.backgroundColor = UIColor.white.withAlphaComponent(1)
}) { _ in
transitionContext.completeTransition(true)
}
}
}

How to enable tab bar transition animation to work without initial view controller

Essentially I have the following custom transition animation for Tab Bar Controller:
MyFadeTransition.swift
import UIKit
class MyFadeTransition: NSObject, UIViewControllerAnimatedTransitioning {
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
if let fromVC = transitionContext.viewController(forKey: .from), let toVC = transitionContext.viewController(forKey: .to) {
toVC.view.frame = fromVC.view.frame
toVC.view.alpha = 0
fromVC.view.alpha = 1
transitionContext.containerView.addSubview(fromVC.view)
transitionContext.containerView.addSubview(toVC.view)
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
toVC.view.alpha = 1
}) { (finished) in
transitionContext.completeTransition(finished)
}
}
}
func animationEnded(_ transitionCompleted: Bool) {
// no-op
}
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.12
}
}
The issue is the code also requires the Tab Bar Controller to be the initial view controller during launch and the code below in the AppDelegate
let tab = window!.rootViewController as! UITabBarController
tab.delegate = self
The following must also be added to AppDelegate.swift
extension AppDelegate: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
let fade = MyFadeTransition()
return fade
}
}
I am using a different ViewController as the Initial ViewController, how can I make the code continue to work without having it as the Initial Controller?
You need to subclass UITabBarController and use
class CustomTab:UITabBarController,UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return MyFadeTransition()
}
}
Then assign CustomTab as class name to the tabBar in IB

Swift Custom Transition Not Working (UIViewControllerTransitioningDelegate not called)

I am just trying to switch to a second view controller. I have my HomeViewController with this extension:
extension HomeViewController: UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController,
presenting: UIViewController,
source: UIViewController)
-> UIViewControllerAnimatedTransitioning? {
return transition
}
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return nil
}
}
and transition defined in the class
let transition = PopAnimator()
I have a button that when it is tapped should switch view controllers and use custom transition (PopAnimator):
#objc func handleTap() {
let viewController = ViewController()
viewController.transitioningDelegate = self
self.present(viewController, animated: false, completion: nil)
}
PopAnimator class (really just a fadeIn for now):
class PopAnimator: NSObject, UIViewControllerAnimatedTransitioning {
let duration = 1.0
var presenting = true
var originFrame = CGRect.zero
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return duration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let toView = transitionContext.view(forKey: .to)!
containerView.addSubview(toView)
toView.alpha = 0.0
UIView.animate(withDuration: duration,
animations: {
toView.alpha = 1.0
},
completion: { _ in
transitionContext.completeTransition(true)
}
)
}
}
When I click the button, it switches viewControllers but doesn't use my custom transition. If I put a breakpoint in my animationController(forPresented:presenting:source:) function or my PopAnimator class it is not reached.
self.present(viewController, animated: false, completion: nil)
try setting animated: true if you want animated transition

Custom UIViewControllerAnimatedTransitioning shows black screen when finished

I have a segue, that is triggered via a button that shows another view controller modally over current context. I'm using a custom class to customize the transition, which is this:
class ShowAnimator: NSObject {
}
extension ShowAnimator: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard
let fromVC = transitionContext.viewController(forKey: .from),
let toVC = transitionContext.viewController(forKey: .to) else {
return
}
let containerView = transitionContext.containerView
containerView.insertSubview(fromVC.view, belowSubview: toVC.view)
let toFrame = transitionContext.finalFrame(for: toVC)
let screenBounds = UIScreen.main.bounds
let bottomLeftCorner = CGPoint(x: screenBounds.width, y: 0)
let finalFrame = CGRect(origin: bottomLeftCorner, size: screenBounds.size)
UIView.animate(withDuration: transitionDuration(using: transitionContext),
animations: {
fromVC.view.frame = finalFrame
},
completion: { _ in
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
)
}
}
In my main view controller, I have these relevant functions:
let showAnimator = ShowAnimator()
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination
destination.transitioningDelegate = self
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return nil
}
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return showAnimator
}
Note that this class is a ViewController as well as a UIViewControllerTransitioningDelegate
However, whenever I press the button to perform my segue, it does the animation properly, but once it's finished it just shows a black screen, rather than the next ViewController.
One problem is this line:
containerView.insertSubview(fromVC.view, belowSubview: toVC.view)
You are inserting the fromVC view. That is wrong. It is already present (though not necessarily in the container).
And you are not inserting the toVC view. That is also wrong. You need to put it into the container.