     /// The UIScrollView used to pan the application pages.
     @IBInspectable
     open let scrollView = UIScrollView()
+    
+    /// Previous scroll view content offset.
+    fileprivate var previousContentOffset: CGFloat = 0
     
     /// An Array of UIViewControllers.
     open var viewControllers: [UIViewController] {

            return
        }
        
        guard let index = tb.buttons.index(of: button) else {
            return
        }
        
        guard index != selectedIndex else {
            return
        }
        
        selectedIndex = index
        
        removeViewControllers()
        prepareViewControllers()

    
    @objc
    open func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        previousContentOffset = scrollView.contentOffset.x
    }
    
    @objc
    open func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let n = viewControllers.count
        let x = scrollView.contentOffset.x
        let p = previousContentOffset == x ? 0 : previousContentOffset < x ? 1 : -1
        
        guard 0 != p else {
            return
        }
        
        let i = selectedIndex + p
        
        guard selectedIndex != i else {
            return
        }
        
        guard 0 <= i && i < n else {
            return
        }
        
        selectedIndex = i
        
        removeViewControllers()
        prepareViewControllers()
