Skull0ne Skull0ne - 1 month ago 19
Swift Question

Swipe direction PageViewController

My problem here it's the swipe movement when I use the pageViewController implementation. I want it to go from right to left but it's going from left to right who's not naturally for the UI experience !

It was working perfectly when I was using Xcode 7 and Swift 2.2 ...
So since I copied/pasted my code and modifiying with the Xcode 8 suggestion, it has this strange behavior.

import UIKit


class FirstEventViewController: UIViewController {

// FIXME: - The swipe gesture has to be from right to left when the view appears (PageControl not showing)
var pageViewController: UIPageViewController!
var titleEvents = [String]()
var pageImages: NSArray!

override func viewDidLoad() {
super.viewDidLoad()

pageImages = NSArray(objects: "supersmashbros","spiritodjsession","thomasdelortrio")
titleEvents = ["supersmashbros","spiritodjsession","thomasdelortrio"]

initiatePgVC()
}

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


// MARK: - Navigation

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//TODO: CODE HERE
}

// MARK: - Private functions
/**
Function initializing the logic of the pageviewController
*/

func initiatePgVC() {
pageViewController = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "PageViewController") as! UIPageViewController
pageViewController.dataSource = self

let startVC = viewControllerAtIndex(index: 0)
let viewControllers = NSArray(object: startVC) as! [UIViewController]

pageViewController.setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)

//pageViewController.view.frame = CGRect(x:0, y:65, width:self.view.frame.width, height:self.view.frame.size.height - 140)
pageViewController.view.frame = self.view.bounds

addChildViewController(pageViewController)
view.addSubview(pageViewController.view)
pageViewController.didMove(toParentViewController: self)
}

/**
Function associating the viewControllers
*/
func viewControllerAtIndex(index: Int)->SecondEventViewController{
if ((titleEvents.count == 0) || (index >= titleEvents.count)){
return SecondEventViewController()
}

let vc:SecondEventViewController = storyboard?.instantiateViewController(withIdentifier: "SecondEventViewController") as! SecondEventViewController

vc.imageFile = pageImages[index] as! String
vc.labelTitle = titleEvents[index]
vc.pageIndex = index

return vc
}
}

extension FirstEventViewController:UINavigationBarDelegate{
func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}
}

extension FirstEventViewController:UIPageViewControllerDataSource{
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let vc = viewController as! SecondEventViewController
var index = vc.pageIndex as Int
if (index == 0 || index == NSNotFound){
return nil
}

index = index-1
return viewControllerAtIndex(index: index)
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let vc = viewController as! SecondEventViewController
var index = vc.pageIndex as Int
if (index == NSNotFound){
return nil
}

index = index+1
if (index == titleEvents.count){
return nil
}
return viewControllerAtIndex(index: index)

}

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

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


}

I once suspected the "
pageViewController.setViewControllers(viewControllers, direction: .forward, animated: true, completion: nil)" because it implements the direction but even if I change with reverse, nothing happens. So if you could give a hand, it would be nice ! Thank you fellows!

Answer

Have you got the changing of the indexes the wrong way around?

for after controller i'd expect the index to increase but you are decreasing it, and vice versa.

Try swapping these, so after should be + 1 and before -1

index = index+1
Comments