Reputation: 3776
I have an application that uses the UIPageViewController nicely to show a bunch of (nearly full screen) cards to the user. However, some users don't get (initially) that they can swipe left/right to go through all the cards (even if the dots are present on the screen, go figure that :).
My idea is to do a small one-time animation that partially swipes to half of the next card and then bounces back to show the user very intuitively that swiping is possible.
I looked at the UIPageViewController API, but I do not see any functions that allow me to this standardly.
What would be the best way to implement this (but still use the standard UIPageViewController)? Would it be possible make send fake gestures into the UIPageViewController?
Upvotes: 1
Views: 956
Reputation: 3776
I found a way that uses the standard behaviours of the UIPageViewController. Basically it's simple, I wait until the view is loaded, and in the viewDidAppear AFTER 0.5 seconds I move to the next page (programatically), almost immediately after (0.25 seconds) - before the first animation is finished - I move back to the previous page.
Below you can see my code, note that I am using a delay function to facility delays in my code, its also included:
///executes the supplied closure after x seconds on the main queue
public func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
class VCPager: VCPagBase, ProtocolVCScanResult, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var product : Product?
var productMatch : ProductMatch?
var disableAlternatives : Bool = false
//the loadView is a proprietary function that just loads the view from the storyboard, nothing special
lazy var vcPage1 = VCPage1.loadView()
lazy var vcPage2 = VCPage2.loadView()
var currentPageIndex : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
dataSource=self
delegate=self
setViewControllers([pageForindex(0)!], direction: .Forward, animated: false, completion: nil)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
hintSwipe()
}
//MARK: - helper functions
func hintSwipe() {
delay(0.5) {
self.carrouselJumpForward()
}
delay(0.75) {
self.carrouselJumpBack()
}
}
func pageForindex(pageIndex : Int) -> UIViewController? {
let vc : VCScanResultPageRoot?
switch(pageIndex) {
case -1 : vc = vcPage2
case 0 : vc = vcPage1
case 1 : vc = vcPage2
case 2 : vc = vcPage1
default : vc = nil
}
vc?.passAlongModelFrom(self)
return vc
}
func indexForPage(vc : UIViewController) -> Int {
if vc == vcPage1 { return 0 }
if vc == vcPage2 { return 1 }
//not found = 0
return 0
}
func carrouselJumpForward() {
currentPageIndex += 1
setViewControllers([self.pageForindex(currentPageIndex)!], direction: .Forward, animated: true, completion: nil)
}
func carrouselJumpBack() {
currentPageIndex += -1
setViewControllers([self.pageForindex(currentPageIndex)!], direction: .Reverse, animated: true, completion: nil)
}
//MARK: - UIPageView
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
let currentPageIndex = indexForPage(viewController)
return pageForindex(currentPageIndex+1)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
let currentPageIndex = indexForPage(viewController)
return pageForindex(currentPageIndex-1)
}
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
guard let vcCurrentPage = previousViewControllers.first where finished else {
return
}
currentPageIndex = indexForPage(vcCurrentPage)
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return 2
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
return currentPageIndex
}
}
Upvotes: 3