gpt4 book ai didi

ios - UIPageViewController 动画表示可以滑动

转载 作者:行者123 更新时间:2023-12-01 18:44:20 26 4
gpt4 key购买 nike

我有一个应用程序很好地使用 UIPageViewController 向用户显示一堆(几乎全屏)卡片。然而,一些用户(最初)并没有得到他们可以向左/向右滑动来浏览所有卡片(即使屏幕上出现了点,请看:)。

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

我查看了 UIPageViewController API,但没有看到任何标准允许我这样做的函数。

实现这一点的最佳方法是什么(但仍使用标准的 UIPageViewController)?是否可以将假手势发送到 UIPageViewController?

最佳答案

我找到了一种使用 UIPageViewController 标准行为的方法。
基本上这很简单,我等到加载 View ,然后在 viewDidAppear AFTER 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
}


}

关于ios - UIPageViewController 动画表示可以滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37768595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com