     
     @objc
     open func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
-        let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
-        scrollViewDirection = 0 > translation.x ? 1 : -1
+        previousContentOffset = scrollView.contentOffset.x
     }
     
     @objc
     open func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
-        let index = selectedIndex + scrollViewDirection
-        guard 0 <= index && index < viewControllers.count else {
-            return
-        }
-        
-        selectedIndex = index
+        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()

}

open class TabMenuController: UIViewController {
    @IBInspectable
    open var selectedIndex = 0
    

    /// 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] {
