Swift: Problems with custom UIView.transition?

2019-08-23 07:19发布

Ok, I have looked through https://developer.apple.com/videos/play/wwdc2013/218/ and SO questions similarly trying to make custom transitions with a tab bar controller and its viewcontrollers but Im running into confusion after figuring out the main steps here.

I need to know how (meaning example code would be really helpful) to call a custom UIView.transition OR just have a custom option in UIView.transition. I need to use this to make a sliding/modal -mimicking transition between tabs in my tab bar controller.

The only way I can get a transition to happen is using the below function (this makes them dissolve):

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

            if selectedViewController == nil || viewController == selectedViewController {
                return false
            }

            let fromView = selectedViewController!.view
            let toView = viewController.view

            UIView.transition(from: fromView!, to: toView!, duration: 0.3, options: [.transitionCrossDissolve], completion: nil)

            return true
        }

And calling it manually here where I programmatically chnage the *selectedIndex* for my tab controller:

//SWITCHES BUTTON -------------------------------------------
    func switchTab(index: Int)

    {
        //transition
        self.tabBarController(self, shouldSelect: (viewControllers?[index])!)

I have read about and tried making a custom class UIViewControllerAnimatedTransitioning but don't know how this would fit in programmatically here -

my attempts passing my tab bar controller and the toView and fromViewinto the custom class result in nothing happening/animating. That is why Ive resorted to UIView.transition here.

How can I make a custom UIView.transition? What can I do here?

1条回答
Bombasti
2楼-- · 2019-08-23 08:01

You should conform to UITabBarControllerDelegate and Create a customTransition class and pass it as follows:

 func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        let animator = TabAnimationManager()
        return    animator

    }

And Class TabAnimationManager should be a subclass of UIPercentDrivenInteractiveTransition and conform UIViewControllerAnimatedTransitioning protocol. You can add your custom animations in that class.

查看更多
登录 后发表回答