xmartlabs / XLPagerTabStrip

Android PagerTabStrip for iOS.
MIT License
6.97k stars 1.33k forks source link

moveToViewController not called when swipe #769

Open noramimiyuma opened 4 years ago

noramimiyuma commented 4 years ago

Environment (version):

Description

moveToViewController(at index: Int, animated: Bool) is called when tap tab items, but not called when swipe to change index. I think that this method has to be called when also swipe to change index. Or my operation has some wrongs?

class MyPageViewController: ButtonBarPagerTabStripViewController {

    // XLPagerTabStrip function: add ChildViewControllers
    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
        let child1VC = CustomViewController()
        let child2VC = CustomViewController()
        return [child1VC, child2VC]
    }

    override func moveToViewController(at index: Int, animated: Bool = true) {
        super.moveToViewController(at: index)
        // This method is called when only to tap tab items,
        // not called when swipe to change index.
    }
}
SDPrio commented 4 years ago

I can confirm this problem. However, I am not sure if this in intended behaviour or a bug?

Anyway, I was looking for a reliable way to get informed about page/ViewController changes. You can use the changeCurrentIndex and changeCurrentIndexProgressive to solve this. In my case changeCurrentIndexProgressive was the best choice:

class MyPageViewController: ButtonBarPagerTabStripViewController {

    // XLPagerTabStrip function: add ChildViewControllers
    override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
        let child1VC = CustomViewController()
        let child2VC = CustomViewController()
        return [child1VC, child2VC]
    }

    func someMethodCalledDuringInit() {
        changeCurrentIndexProgressive = { (oldCell, newCell, progressPercentage, changeCurrentIndex, animated) in
            if progressPercentage >= 1 {
                 pring("Page switch complete")
            }
        }
    }
}