2016-11-08 71 views
2

我現在有一個UIPageController其正確地檢測在滾動UIPageController - 檢測當前頁面如果刷卡取消(SWIFT 3 *)

上一個或下一個的PageIndex我的問題,而我需要的是一個解決方案。 ..

示例: - 用戶向左或向右滾動,但隨後決定在滑動過程中取消...因此保留在當前頁面上。

的PageIndex的更改未定頁面(見下頁),而不是實際的當前頁面..

目前我的代碼來實現上述聲明: -

func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) { 

    if let itemController = pendingViewControllers[0] as? PageContentViewController { 

     nextIndex = itemController.pageIndex 

     print("Page index = \(nextIndex)") 
    } 
} 

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) { 
     currentIndex = nextIndex 
     print("Current Page = \(currentIndex)") 
    } 


func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? { 



    let itemController = viewController as! PageContentViewController 

    if itemController.pageIndex < numberOfPages { 
     return getItemController(itemController.pageIndex+1) 
    } 

    return nil 

} 



func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? { 

    let itemController = viewController as! PageContentViewController 

    if itemController.pageIndex > 0 { 

     return getItemController(itemController.pageIndex-1) 
    } 

    return nil 
} 

func getItemController(_ itemIndex: Int) -> PageContentViewController? { 

    if itemIndex < numberOfPages { 
     let pageItemController = self.storyboard!.instantiateViewController(withIdentifier: "Page1") as! PageContentViewController 
     pageItemController.pageIndex = itemIndex 

     return pageItemController 
    } 

    return nil 
} 

我想要得到的頁面顯示之前的頁碼,以便我可以顯示特定的對象等。

viewControllerAfter和viewControllerBefore似乎不會被每一次調用,因此我不能依賴這個委託方法

回答

1

我不知道我怎麼錯過了這一點,但在didFinishAnimating方法是自己完成塊

因此下面的代碼固定我的問題

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

    if completed 
    { 
     currentIndex = nextIndex 

    } 

    print("Current Page = \(currentIndex)") 
}