0

This function is never printing:

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

        print("completed or finished")
    }
}

why?

entire code:

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!
}
}

I want to know when a page transition has completed, I've implemented the didFinishAnimating method from UIPageViewController, but it's seems not working properly.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Felipe
  • 150
  • 1
  • 16

1 Answers1

1

That's because:

pageViewController(_:didFinishAnimating:previousViewControllers:transitionCompleted:)

method relates to UIPageViewControllerDelegate

which means that you should implement -in addition to 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
}

And it should work as expected. Also, make sure to let the class to conform to UIPageViewControllerDelegate:

class AdsPageController: UIPageViewController,
UIPageViewControllerDataSource, UIPageViewControllerDelegate

Furthermore, you could check this answer to be more familiar with Delegation.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143