func UIPageViewController didFinishAnimating无效

时间:2017-11-19 13:38:43

标签: ios swift swift3 uipageviewcontroller

此功能永远不会打印:

func pageViewController(_ pageViewController: UIPageViewController,
                        didFinishAnimating finished: Bool,
                        previousViewControllers: [UIViewController],
                        transitionCompleted completed: Bool){
    if completed||finished{

        print("completed or finished")
    }
}

为什么?

整个代码:

class AdsPageController: UIPageViewController, 
UIPageViewControllerDataSource
{
var AdsContent: AdsController? = nil
static var adsPageController:AdsPageController?

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSource = self
    AdsPageController.adsPageController = self
}

override func viewWillAppear(_ animated: Bool) {
    self.setViewControllers([getViewControllerAtIndex(pageIndex: MyUtils.loadAdIndex())] as [UIViewController], direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
}

// MARK: UIPageViewControllerDataSource Methods
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
{
    self.AdsContent = viewController as? AdsController
    var index = AdsContent!.pageIndex
    if ((index == 0) || (index == NSNotFound))
    {
        index = 10
    }
    index-=1
    return getViewControllerAtIndex(pageIndex: index)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
    self.AdsContent = viewController as? AdsController
    var index = AdsContent!.pageIndex
    if ((index == 9) || index == NSNotFound)
    {
        index = -1
    }
    index+=1
    return getViewControllerAtIndex(pageIndex: index)
}

func pageViewController(_ pageViewController: UIPageViewController,
                        didFinishAnimating finished: Bool,
                        previousViewControllers: [UIViewController],
                        transitionCompleted completed: Bool){
    if completed||finished{
        print("completed or finished")
    }
}

func getViewControllerAtIndex(pageIndex: NSInteger) -> AdsController{
    let AdsContent: AdsController? = self.storyboard?.instantiateViewController(withIdentifier: "AdViewP") as? AdsController
    AdsContent?.pageIndex = pageIndex
    return AdsContent!
}
}

我想知道页面转换何时完成,我已经从UIPageViewController实现了didFinishAnimating方法,但它似乎无法正常工作。

1 个答案:

答案 0 :(得分:1)

那是因为:

pageViewController(_:didFinishAnimating:previousViewControllers:transitionCompleted:)

方法与UIPageViewControllerDelegate

有关

这意味着您应该实施-in self.dataSource = self - :

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataSource = self
    // here you conform to the delegate (note that you could implement it without mentioning 'self.'):
    delegate = self
    AdsPageController.adsPageController = self
}

它应该按预期工作。另外,请确保让课程符合UIPageViewControllerDelegate

class AdsPageController: UIPageViewController,
UIPageViewControllerDataSource, UIPageViewControllerDelegate

此外,您可以检查this answer是否更熟悉代表团。