我有一个应用程序,可以很好地使用UIPageViewController向用户显示一堆(几乎全屏)卡片。但是,有些用户(最初)无法向左/向右滑动以浏览所有卡片(即使屏幕上有点,请按照以下说明进行操作:)。
我的想法是做一个小的一次性动画,部分滑动到下一张卡的一半然后反弹,以非常直观地向用户显示可以滑动。
我查看了UIPageViewController API,但是我没有看到任何允许我这样做的功能。
实现这个的最佳方法是什么(但仍然使用标准的UIPageViewController)?是否可以将伪造的手势发送到UIPageViewController?
答案 0 :(得分:2)
我找到了一种使用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
}
}