ViewController slide animation

后端 未结 3 2053
花落未央
花落未央 2020-12-09 19:56

I want to create an animation like the iOS app facebook at tabswitch[1]. I have already tried to develop some kind of animation, the problem that

3条回答
  •  难免孤独
    2020-12-09 20:39

    You can use the following idea: https://samwize.com/2016/04/27/making-tab-bar-slide-when-selected/

    Also, here's the code updated to Swift 4.1 and I also removed the force unwrappings:

    import UIKit
    
    class MyTabBarController: UITabBarController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            delegate = self
        }
    }
    
    extension MyTabBarController: UITabBarControllerDelegate  {
        func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
            guard let tabViewControllers = tabBarController.viewControllers, let toIndex = tabViewControllers.index(of: viewController) else {
                return false
            }
            animateToTab(toIndex: toIndex)
            return true
        }
    
        func animateToTab(toIndex: Int) {
            guard let tabViewControllers = viewControllers,
                let selectedVC = selectedViewController else { return }
    
            guard let fromView = selectedVC.view,
                let toView = tabViewControllers[toIndex].view,
                let fromIndex = tabViewControllers.index(of: selectedVC),
                fromIndex != toIndex else { return }
    
    
            // Add the toView to the tab bar view
            fromView.superview?.addSubview(toView)
    
            // Position toView off screen (to the left/right of fromView)
            let screenWidth = UIScreen.main.bounds.size.width
            let scrollRight = toIndex > fromIndex
            let offset = (scrollRight ? screenWidth : -screenWidth)
            toView.center = CGPoint(x: fromView.center.x + offset, y: toView.center.y)
    
            // Disable interaction during animation
            view.isUserInteractionEnabled = false
    
            UIView.animate(withDuration: 0.3,
                           delay: 0.0,
                           usingSpringWithDamping: 1,
                           initialSpringVelocity: 0,
                           options: .curveEaseOut,
                           animations: {
                            // Slide the views by -offset
                            fromView.center = CGPoint(x: fromView.center.x - offset, y: fromView.center.y)
                            toView.center = CGPoint(x: toView.center.x - offset, y: toView.center.y)
    
            }, completion: { finished in
                // Remove the old view from the tabbar view.
                fromView.removeFromSuperview()
                self.selectedIndex = toIndex
                self.view.isUserInteractionEnabled = true
            })
        }
    }
    

    So, you need to subclass UITabBarController and you also have to write the animation part, you can tweak the animation options (delay, duration, etc).

    I hope it helps, cheers!

提交回复
热议问题