UIPageViewController 动画指示滑动是可能的

UIPageViewController animation to indicate swipe is possible

我有一个应用程序可以很好地使用 UIPageViewController 向用户显示一堆(几乎全屏)卡片。但是,有些用户(最初)并不知道他们可以滑动 left/right 来浏览所有卡片(即使屏幕上出现了圆点,请想想 :)。

我的想法是做一个小的一次性动画,部分滑动到下一张卡片的一半,然后弹回以非常直观地向用户展示滑动是可能的。

我查看了 UIPageViewController API,但我没有看到任何允许我按标准执行此操作的函数。

实现这个的最佳方法是什么(但仍然使用标准的 UIPageViewController)?是否可以向 UIPageViewController 发送虚假手势?

我找到了一种使用 UIPageViewController 标准行为的方法。 基本上很简单,我等到视图加载完毕,然后在 viewDidAppear 0.5 秒后我移动到下一页(以编程方式),几乎在(0.25 秒)之后 - 在第一个动画完成之前 - 我移回上一页页。

你可以在下面看到我的代码,请注意,我在我的代码中使用延迟函数来实现延迟,它还包括:

///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
    }


}