此函数从不打印:

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方法,但它似乎无法正常工作。

最佳答案

那是因为:
pageViewController(_:didFinishAnimating:previousViewControllers:transitionCompleted:)
方法与UIPageViewControllerDelegate
这意味着您应该实现-除了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以更熟悉委派。

关于ios - func UIPageViewController didFinishAnimating不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47377499/

10-14 23:20