ViewPager on iOS with Swift 3

1

Hello, I'm developing an iPhone application using Swift 3 , and I'm having some questions regarding browsing between pages within a ViewController .

My application for android I have a Activity using a ViewPager with two fragments . Each fragment differs in implementation, performing actions different from each other. The fragments also has the reference of activity parent. Here's a sample of how the layout is.

My question is how can I do this in Swift ?

    
asked by anonymous 31.03.2017 / 01:34

1 answer

0

Use UIPageViewController . For example:

import UIKit

class MyPageViewController : UIPageViewController {

fileprivate var currentIndex = 0 

// Setup
private func setup() {

    // Delegation
    self.delegate = self
    self.dataSource = self

    // Init
    self.reloadPages(index: 0)
}

// MARK: - Lifecycle Methods
override func viewDidLoad() {
    super.viewDidLoad()
    self.setup()
}

// MARK: - Private Methods
fileprivate func reloadPages(index: Int) {
    if  let firstVc = self.cellForViewAt(index: index) {
        self.setViewControllers([firstVc], direction: .forward, animated: true, completion: nil)
    }
}
}

// MARK: - Cell
extension MyPageViewController {

fileprivate func cellForViewAt(index: Int) -> UIViewController? {
    let vc = MyStoryboard().myCellViewController()
    vc.pageIndex = index
    return vc
}
}

// MARK: - Page methods
extension MyPageViewController : UIPageViewControllerDelegate, UIPageViewControllerDataSource {

func pageViewController(_ pageViewController: UIPageViewController, spineLocationFor orientation: UIInterfaceOrientation) -> UIPageViewControllerSpineLocation {
    return .none
}

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

    if  let controller = viewController as? PageViewControllerProperties {
        let newIndex = controller.pageIndex - 1
        return self.cellForViewAt(index: newIndex)
    }

    return nil
}

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

    if  let controller = viewController as? PageViewControllerProperties {
        let newIndex = controller.pageIndex + 1
        return self.cellForViewAt(index: newIndex)
    }

    return nil
}

func pageViewControllerPreferredInterfaceOrientationForPresentation(_ pageViewController: UIPageViewController) -> UIInterfaceOrientation {
    return .portrait
}

func pageViewControllerSupportedInterfaceOrientations(_ pageViewController: UIPageViewController) -> UIInterfaceOrientationMask {
    return .portrait
}

func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
    if  completed {
        // Complete action
    }
}

func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
    // Expecting just 1
    if  let vc = pendingViewControllers.first as? PageViewControllerProperties {
        self.currentIndex = vc.pageIndex
    }
}
}

// MARK: - Um protocolo simples para registrar o pageIndex em todas as classes-células
protocol PageViewControllerProperties : class {
    var pageIndex : Int { get set }
}
    
31.03.2017 / 05:02