如何指示用户滑动的方向,而不是Swift中的下一个和上一个视图控制器?

时间:2015-01-13 16:15:29

标签: ios swift uipageviewcontroller

如何指示用户滑动的方向,而不是下一个和上一个视图控制器?

我已成功设置UIPageViewControllerDataSource代表。

gif1

这些函数不指示用户滑动的方向,而是指示下一个和上一个视图控制器。

func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?

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

我尝试使用这种方法:

func pageViewController(_ pageViewController: UIPageViewController,
          didFinishAnimating finished: Bool,
     previousViewControllers previousViewControllers: [AnyObject],
         transitionCompleted completed: Bool)
UIPageViewControllerDelegate附带的

我发现将此方法集成到我使用的代码非常困难,如下所示:

import UIKit

class ProView: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

var pageViewController: UIPageViewController?

let characterImages = ["character1", "character2"]

override func viewDidLoad() {
    super.viewDidLoad()
    createPageViewController()
    setupPageControl()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func pageViewController(pageViewController: UIPageViewController,
    viewControllerAfterViewController ProView: UIViewController) -> UIViewController? {

        let itemController = ProView as PageItemController

        if itemController.itemIndex+1 < characterImages.count {
            return getItemController(itemController.itemIndex+1)
        }

        return nil
}

func pageViewController(pageViewController: UIPageViewController,
    viewControllerBeforeViewController ProView: UIViewController) -> UIViewController? {

    let itemController = ProView as PageItemController

        if itemController.itemIndex > 0 {
            return getItemController(itemController.itemIndex-1)
        }

        return nil
}

private func getItemController(itemIndex: Int) -> PageItemController? {

    if itemIndex < characterImages.count {
        let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as PageItemController
        pageItemController.itemIndex = itemIndex
        pageItemController.imageName = characterImages[itemIndex]
        return pageItemController
}

    return nil
}

func createPageViewController() {

    let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as UIPageViewController
    pageController.dataSource = self

    if characterImages.count > 0 {
        let firstController = getItemController(0)!
        let startingViewControllers: NSArray = [firstController]
        pageController.setViewControllers(startingViewControllers, direction:     UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
    }

    pageViewController = pageController
    addChildViewController(pageViewController!)
    self.view.addSubview(pageViewController!.view)
    pageViewController?.didMoveToParentViewController(self)
}

func setupPageControl() {
    let appearance = UIPageControl.appearance()
    appearance.pageIndicatorTintColor = UIColor.grayColor()
    appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return characterImages.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0
}

func pageViewController(pageViewController: UIPageViewController,
    didFinishAnimating finished: Bool,
    previousViewControllers ProView: [AnyObject],
    transitionCompleted completed: Bool) {
        if completed {
            println("completed!")
        } else {
            println("not completed")
        }
}

}

class PageItemController: UIViewController {

@IBOutlet weak var imageCharacterChoose: UIImageView!

var itemIndex: Int = 0
var imageName: String = "" {
    didSet {

        if let imageView = imageCharacterChoose {imageCharacterChoose.image = UIImage(named: imageName)
        }

    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    imageCharacterChoose!.image = UIImage(named: imageName)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

我需要获取用户正在浏览的itemIndex视图控制器,但该怎么做?我该如何正确使用该方法?

谢谢!

1 个答案:

答案 0 :(得分:1)

这个答案解决了我的问题:

https://stackoverflow.com/a/27934069/1135714

您需要设置多个变量并通过UIPageController方法修改其值。