使用Swift 3在iOS中使用Android ViewPager的替代方案 [英] Alternative for Android ViewPager in iOS with Swift 3

查看:137
本文介绍了使用Swift 3在iOS中使用Android ViewPager的替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Swift 3为iPhone开发应用程序,我对ViewController中的页面之间的导航有一些疑问。

i'm developing an application for iPhone using Swift 3 and i have some doubts about the navigation between pages inside a ViewController.

在我的Android应用程序上,我有使用ViewPager com两个片段的活动。每个片段都有不同的实现,并与主要活动进行交互。

On my android application, i have an activity using a ViewPager com two fragments. Each fragments have a different implementation and interacts with the main activity.

像这样:

,结果是:

我的问题是我如何在swift中做到这一点?

My question is how i can do it in swift?

推荐答案

ios中没有直接控件,无论如何你想要实现与android视图分页器功能和效果相同,使用下面的控件

There is no direct control available in ios, Anyway if you want achieve as same as android view pager functionalities and effects, use below controls

MainViewController{
//Design your main controller
// Place Container view in main controller (You can give custom height for container view)

}

create new view controller which extends UIPageViewController 
MyPagerViewController : UIPageViewController{
// this will be directly connected to container view of Main view controller
}

参考图片

MyPagerViewController :  UIPageViewController {

 var selectedBlock:Int = 1

 required init?(coder aDecoder: NSCoder) {
        super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)

    }

override func viewDidLoad() {
        super.viewDidLoad()

        dataSource = self;
        delegate = self;

        // Setting First View
        if let firstViewController = orderedViewControllers.first {
            setViewControllers([firstViewController],
                               direction: .forward,
                               animated: true,
                               completion: nil)
        }
    }


  // List of UIView Controllers
    private(set) lazy var orderedViewControllers: [UIViewController] = {
        return [self.newColoredViewController(name: "Green"),
                self.newColoredViewController(name: "Yellow")]
    }()

    private func newColoredViewController(name: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil) .
            instantiateViewController(withIdentifier: "\(name)ViewController")
    }
}

extension MyPagerViewController: UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    func pageViewController(_ pageViewController: UIPageViewController,
                            viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }

        let previousIndex = viewControllerIndex - 1

        //selectedBlock = viewControllerIndex+1

        // User is on the first view controller and swiped left to loop to
        // the last view controller.
        guard previousIndex >= 0 else {
            return nil//orderedViewControllers.last
        }

        guard orderedViewControllers.count > previousIndex else {
            return nil
        }
        return orderedViewControllers[previousIndex]
    }

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




        guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
            return nil
        }

        let nextIndex = viewControllerIndex + 1

        //selectedBlock = viewControllerIndex+1

       // print("**********" + String(selectedBlock));

        let orderedViewControllersCount = orderedViewControllers.count

        // User is on the last view controller and swiped right to loop to
        // the first view controller.
        guard orderedViewControllersCount != nextIndex else {
            return nil//orderedViewControllers.first
        }        
        guard orderedViewControllersCount > nextIndex else {
            return nil
        }        
        return orderedViewControllers[nextIndex]enter code here
    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool){
        if(completed){
            if pageViewController.viewControllers![0] is GreenViewController {
                selectedBlock = 1 // this is global variable
            }else{
                selectedBlock = 2
            }
        }        
    }    
}

这篇关于使用Swift 3在iOS中使用Android ViewPager的替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆