Skip to content

Instantly share code, notes, and snippets.

@cacheleocode
Created September 14, 2017 21:26
Show Gist options
  • Save cacheleocode/779a32a5b4f624aae02157c42785eb98 to your computer and use it in GitHub Desktop.
Save cacheleocode/779a32a5b4f624aae02157c42785eb98 to your computer and use it in GitHub Desktop.
faking it
// MARK: UIPageViewControllerDataSource
extension PageViewController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
self.swipeCount = self.swipeCount + 1
print("swipe count = \(self.swipeCount)")
let previousIndex = viewControllerIndex - 1
if swipeMode {
self.swipeMode = true
print("swipe mode = \(self.swipeMode)")
// User is on the first view controller and swiped left to loop to
// the last view controller.
if previousIndex >= 0 {
return orderedViewControllers[previousIndex]
} else {
return orderedViewControllers.last
}
} else {
self.swipeMode = true
print("swipe mode = \(self.swipeMode)")
let when = DispatchTime.now() + 1
DispatchQueue.main.asyncAfter(deadline: when) {
if self.swipeCount == 1 {
let when = DispatchTime.now() + 4
DispatchQueue.main.asyncAfter(deadline: when) {
self.swipeMode = false
print("swipe mode = \(self.swipeMode)")
self.swipeCount = 0
print("swipe count = \(self.swipeCount)")
self.scrollToViewController(viewController, direction: .forward)
}
} else {
let when = DispatchTime.now() + 4
DispatchQueue.main.asyncAfter(deadline: when) {
self.swipeMode = false
print("swipe mode = \(self.swipeMode)")
self.swipeCount = 0
print("complete swipe count = \(self.swipeCount)")
}
}
}
}
if orderedViewControllers.count > previousIndex {
return nil
} else {
return orderedViewControllers[previousIndex]
}
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
self.swipeCount = self.swipeCount + 1
print("swipe count = \(self.swipeCount)")
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
if swipeMode {
self.swipeMode = true
print("swipe mode = \(self.swipeMode)")
// User is on the last view controller and swiped right to loop to
// the first view controller.
if orderedViewControllersCount != nextIndex {
return orderedViewControllers[nextIndex]
} else {
return orderedViewControllers.first
}
} else {
self.swipeMode = true
print("swipe mode = \(self.swipeMode)")
let when = DispatchTime.now() + 1
DispatchQueue.main.asyncAfter(deadline: when) {
if self.swipeCount == 1 {
let when = DispatchTime.now() + 4
DispatchQueue.main.asyncAfter(deadline: when) {
self.swipeMode = false
print("swipe mode = \(self.swipeMode)")
self.swipeCount = 0
print("complete swipe count = \(self.swipeCount)")
self.scrollToViewController(viewController, direction: .reverse)
}
} else {
let when = DispatchTime.now() + 4
DispatchQueue.main.asyncAfter(deadline: when) {
self.swipeMode = false
print("swipe mode = \(self.swipeMode)")
self.swipeCount = 0
print("complete swipe count = \(self.swipeCount)")
}
}
}
}
if orderedViewControllersCount > nextIndex {
return nil
} else {
return orderedViewControllers[nextIndex]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment