Ramotion / animated-tab-bar

:octocat: RAMAnimatedTabBarController is a Swift UI module library for adding animation to iOS tabbar items and icons. iOS library made by @Ramotion
https://www.ramotion.com/animated-tab-bar-ios-app-development-ui-library/
MIT License
11.13k stars 1.33k forks source link

Back to top function along with animation #129

Closed kwstasna closed 6 years ago

kwstasna commented 8 years ago

Hello. I'm currently using your library to make animations to my tap bar items. My hierarcy is this UINavigationTabBar > UINavigationController > UIViewController Now the animation works perfect. But i wanted to implement some function in my "Home" tab bar item to go to the top of the UICollectionView if its active and its tapped. Something like Instagram - Facebook has. So i created a swift file for the tap bar controller and there i implemented this code

import UIKit import RAMAnimatedTabBarController

class tapBarController: UITabBarController, UITabBarControllerDelegate {

    /// Determines whether the scrolling capability's enabled.
    var scrollEnabled: Bool = true

    private var previousIndex = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        delegate = self
    }

    /*
     Always call "super" if you're overriding this method in your subclass.
     */
    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
        guard scrollEnabled else {
            return
        }

        guard let index = viewControllers?.indexOf(viewController) else {
            return
        }

        if index == previousIndex {
            var scrollViews = [UIScrollView]()

            dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), { [weak self] () in

                self?.iterateThroughSubviews(self?.view) { (scrollView) in
                    scrollViews.append(scrollView)
                }

                guard let scrollView = scrollViews.first else {
                    return
                }

                dispatch_async(dispatch_get_main_queue(), {
                    scrollView.setContentOffset(CGPointZero, animated: true)
                })
                })
        }

        previousIndex = index
    }

    /*
     Iterates through the view hierarchy in an attempt to locate a UIScrollView with "scrollsToTop" enabled.
     Since the functionality relies on "scrollsToTop", it plugs easily into existing architectures - you can
     control the behaviour by modifying "scrollsToTop" on your UIScrollViews.
     */
    private func iterateThroughSubviews(parentView: UIView?, onRecognition: (UIScrollView) -> Void) {
        guard let view = parentView else {
            return
        }

        for subview in view.subviews {
            if let scrollView = subview as? UIScrollView where scrollView.scrollsToTop == true {
                onRecognition(scrollView)
            }

            iterateThroughSubviews(subview, onRecognition: onRecognition)
        }
    }
}

From Storyboard i deleted the class of RAMAnimatedTabBarController and I inserted the name of my new swift class tapBarController. Of course the back to top functionality works but the animation has stopped working. Wanted to ask you how can i implement the back to top function with your animations? Hope you can help me with this and also i'm trying to figure out.

P.S. It would be nice if you have also this function in any update !

kuyazee commented 8 years ago

I think you need to subclass "RAMAnimatedTabBarController" to your tapBarController.

0ber commented 6 years ago

For that you need to subclass you tabbarcontrollerto RAMAnimatedTabBarController Then in the subclass ovveride the super class' method 'tapHandler'

override func tapHandler(_ gesture: UIGestureRecognizer) {
        super.tapHandler(gesture)

        let currentNavigationController = ...
        currentNavigationController.popToRootViewController(animated: false)
}