Swift: Problems with custom UIView.transition?

ぃ、小莉子 提交于 2019-12-11 18:30:03

问题


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:


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.



来源:https://stackoverflow.com/questions/45929116/swift-problems-with-custom-uiview-transition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!