如何让uipageviewcontroller进入圈子

时间:2014-05-13 10:57:47

标签: ios objective-c swift uipageviewcontroller

我有一个显示不同页面的UIPageViewController。当前的行为是当我到达最后一页时它停止滚动。我现在想要实现的是,当在最后一页并向右滚动时,它会转到第一页。在第一页时,向左滚动时转到最后一页。所以基本上让PageViewController以圆圈形式显示页面。我的第一种方法在有多个页面或从一个页面开始时效果很好:

- (UIViewController*) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(TMCollectionViewController *)viewController
{

    if((viewController.pageIndex) >= 1)
    {
        return _viewControllers[viewController.pageIndex - 1];
    }
    else
    {
        if ([_viewControllers count] == 1) {
            return nil;
        }
        return [_viewControllers objectAtIndex:[_viewControllers count]-1];
    }
}

但是当我删除页面以便只剩下一个页面时,它仍会记住页面之前或之后,并相应地显示它。即使阵列中只剩下一页。任何帮助都非常感谢。

3 个答案:

答案 0 :(得分:12)

您可以将其实现为

当您在第一个索引上并向左滑动时,使其索引=总页数

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

    if ((index == 0) || (index == NSNotFound)) {
        index = self.pageTitles.count;
    }

    index--;
    return [_viewControllers objectAtIndex:index];
}

当您使用最后一个索引时,将其设置为零索引

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

    if (index == NSNotFound) {
        return nil;
    }

    index++;
    if (index == [self.pageTitles count]) {
        index = 0;
    }
    return [_viewControllers objectAtIndex:index];
}

答案 1 :(得分:4)

谢谢,@罗斯尼

Swift 2 - Roshni的解决方案

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    var index: Int = (viewController as! PageContentViewController).index

    if index == 0 || index == NSNotFound {
        index = self.pageTitles.count
    }

    index--
    return viewControllerAtIndex(index)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
    var index: Int = (viewController as! PageContentViewController).index

    if index == NSNotFound {
        return nil
    }

    index++
    if index == self.pageTitles.count {
        index = 0
    }
    return viewControllerAtIndex(index)
}

答案 2 :(得分:2)

这里是样本,我做了圆形分页

我正在使用使用UIPageViewController to create a content slider (Objective-C/Swift)文章但是感谢@Tom Calmon和@Roshani现在我能够在 Swift 2.2

上实现我想要的结果
class PageHomeViewController: UIViewController , UIPageViewControllerDataSource{

    // MARK: - Variables
    private var pageViewController: UIPageViewController?

    // Initialize it right away here
    private let contentImages = ["nature_pic_1.png",
                                 "nature_pic_2.png",
                                 "nature_pic_3.png",
                                 "nature_pic_4.png"];

    //MARK: - View controller life cycle methods
    override func viewDidLoad() {
        super.viewDidLoad()
        createPageViewController()
        setupPageControl()
    }

   //MARK: - Create and start page view controller
    private func createPageViewController() {
        let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
        pageController.dataSource = self

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

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

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

    // MARK: - UIPageViewControllerDataSource
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        var index: Int = (viewController as! PageContentViewController).itemIndex

        if index == 0 || index == NSNotFound {
            index = contentImages.count
        }

        index -= 1
        return getItemController(index)
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?{
        var index: Int = (viewController as! PageContentViewController).itemIndex
        if index == NSNotFound {return nil}
        index += 1
        if index == contentImages.count {index = 0}
        return getItemController(index)
    }

    private func getItemController(itemIndex: Int) -> PageContentViewController? {
        if itemIndex < contentImages.count {
            let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("PageContentViewController") as! PageContentViewController
            pageItemController.itemIndex = itemIndex
            pageItemController.imageName = contentImages[itemIndex]
            return pageItemController
        }
        return nil
    }

    // MARK: - Page Indicator
    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return contentImages.count
    }
    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        return 0
    }

}

enter image description here

希望有一天能帮到那里有人!!