Ios 在UIPageViewController中获取用户滑动方向
Ios 在UIPageViewController中获取用户滑动方向,ios,swift,uipageviewcontroller,Ios,Swift,Uipageviewcontroller,UIPageViewControllerDataSource的viewControllerBeforeViewController和viewcontrollerbeforviewcontroller这两种方法不指示滑动方向 UIPageViewController委托的方法transitionCompleted,对我们也没有多大帮助。它只是告诉你页面是否被完全刷过 那么,我应该使用什么方法来准确地检测用户方向(左或右) 这两个属性可能会有所帮助: let directionForward = U
UIPageViewControllerDataSource
的viewControllerBeforeViewController
和viewcontrollerbeforviewcontroller
这两种方法不指示滑动方向
UIPageViewController
委托的方法transitionCompleted
,对我们也没有多大帮助。它只是告诉你页面是否被完全刷过
那么,我应该使用什么方法来准确地检测用户方向(左或右)
这两个属性可能会有所帮助:
let directionForward = UIPageViewControllerNavigationDirection.Forward
let directionReverse = UIPageViewControllerNavigationDirection.Reverse
我的代码如下所示:
import UIKit
class ProView: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var pageViewController: UIPageViewController?
let characterImages = ["character1", "character2", "character1", "character2", "character1", "character2", "character1", "character2"]
override func viewDidLoad() {
super.viewDidLoad()
createPageViewController()
setupPageControl()
character = 1
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// Forward, check if this IS NOT the last controller
func pageViewController(pageViewController: UIPageViewController,
viewControllerAfterViewController ProView: UIViewController) -> UIViewController? {
let itemController = ProView as PageItemController
// Check if there is another view
if itemController.itemIndex+1 < characterImages.count {
return getItemController(itemController.itemIndex+1)
}
return nil
}
// Check if this IS NOT the first controller
func pageViewController(pageViewController: UIPageViewController,
viewControllerBeforeViewController ProView: UIViewController) -> UIViewController? {
let itemController = ProView as PageItemController
if itemController.itemIndex < 0 {
return getItemController(itemController.itemIndex-1)
}
return nil
}
private func getItemController(itemIndex: Int) -> PageItemController? {
if itemIndex < characterImages.count {
let pageItemController = self.storyboard!.instantiateViewControllerWithIdentifier("ItemController") as PageItemController
pageItemController.itemIndex = itemIndex
pageItemController.imageName = characterImages[itemIndex]
return pageItemController
}
return nil
}
func createPageViewController() {
let pageController = self.storyboard!.instantiateViewControllerWithIdentifier("PageController") as UIPageViewController
pageController.dataSource = self
pageController.delegate = self
if characterImages.count > 0 {
let firstController = getItemController(0)!
let startingViewControllers: NSArray = [firstController]
pageController.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}
pageViewController = pageController
addChildViewController(pageViewController!)
self.view.addSubview(pageViewController!.view)
pageViewController?.didMoveToParentViewController(self)
}
func setupPageControl() {
let appearance = UIPageControl.appearance()
appearance.pageIndicatorTintColor = UIColor.grayColor()
appearance.currentPageIndicatorTintColor = UIColor.whiteColor()
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return characterImages.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
}
// BETA
func pageViewController(PageItemController: UIPageViewController,
didFinishAnimating finished: Bool,
previousViewControllers pageViewController: [AnyObject],
transitionCompleted completed: Bool)
{
if (!completed)
{
// You do nothing because whatever page you thought
// the book was on before the gesture started is still the correct page
return;
}
// This is where you would know the page number changed and handle it appropriately
character = workaround
}
}
class PageItemController: UIViewController {
@IBOutlet weak var imageCharacterChoose: UIImageView!
var itemIndex: Int = 0
var imageName: String = "" {
didSet {
if let imageView = imageCharacterChoose {imageCharacterChoose.image = UIImage(named: imageName)
}
}
}
}
无法确定用户是向前还是向后滑动 您需要使用以下两种方法提供前后视图: pageViewController:ViewController预览控制器: pageViewController:viewControllerAfterViewController:
您可以通过向ViewController添加属性来跟踪索引,然后可以比较页面索引。您应该同时使用
WillTransitionOnViewController:
和didFinishAnimating:
委托方法来确定转换是向前还是向后。在类的开头声明两个索引变量,例如currentIndex
和nextIndex
(两者都是Int
)
在willTransitionOnViewControllers
方法中,将nextIndex
设置为挂起的视图控制器的itemIndex
:
编辑
func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [AnyObject]) {
// the page view controller is about to transition to a new page, so take note
// of the index of the page it will display. (We can't update our currentIndex
// yet, because the transition might not be completed - we will check in didFinishAnimating:)
if let itemController = pendingViewControllers[0] as? PageItemController {
nextIndex = itemController.itemIndex
}
}
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [AnyObject], transitionCompleted completed: Bool) {
if (completed) {
// the page view controller has successfully transitioned to the next view
// controller, so we can update our currentIndex to the value we obtained
// in the willTransitionToViewControllers method:
currentIndex = nextIndex
}
}
结束编辑
func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [AnyObject]) {
// the page view controller is about to transition to a new page, so take note
// of the index of the page it will display. (We can't update our currentIndex
// yet, because the transition might not be completed - we will check in didFinishAnimating:)
if let itemController = pendingViewControllers[0] as? PageItemController {
nextIndex = itemController.itemIndex
}
}
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [AnyObject], transitionCompleted completed: Bool) {
if (completed) {
// the page view controller has successfully transitioned to the next view
// controller, so we can update our currentIndex to the value we obtained
// in the willTransitionToViewControllers method:
currentIndex = nextIndex
}
}
此时,您可以计算转换是向前还是向后:如果nextIndex
>currentIndex
,则向前;如果nextIndex
<currentIndex
则向后。然后在didFinishAnimating
中,如果completed
为true(因此它完成了到下一个视图控制器的转换),则将currentIndex
设置为等于nextIndex
,以便您可以在需要指示屏幕上当前页面的任何位置使用currentIndex
:
编辑
func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [AnyObject]) {
// the page view controller is about to transition to a new page, so take note
// of the index of the page it will display. (We can't update our currentIndex
// yet, because the transition might not be completed - we will check in didFinishAnimating:)
if let itemController = pendingViewControllers[0] as? PageItemController {
nextIndex = itemController.itemIndex
}
}
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [AnyObject], transitionCompleted completed: Bool) {
if (completed) {
// the page view controller has successfully transitioned to the next view
// controller, so we can update our currentIndex to the value we obtained
// in the willTransitionToViewControllers method:
currentIndex = nextIndex
}
}
请注意,这些方法的第一个参数是pageViewController(UIPageViewController的实例),而不是当前代码中的pageItemController
最后,需要注意的是:您引用的枚举(UIPageViewControllerNavigationDirection
)仅在setViewControllers(u,direction:)
方法中使用
结束编辑假设您有一个ViewController的本地数组,您可以使用此
委托
方法:
func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: [AnyObject]) {
// the page view controller is about to transition to a new page, so take note
// of the index of the page it will display. (We can't update our currentIndex
// yet, because the transition might not be completed - we will check in didFinishAnimating:)
if let itemController = pendingViewControllers[0] as? PageItemController {
nextIndex = itemController.itemIndex
}
}
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [AnyObject], transitionCompleted completed: Bool) {
if (completed) {
// the page view controller has successfully transitioned to the next view
// controller, so we can update our currentIndex to the value we obtained
// in the willTransitionToViewControllers method:
currentIndex = nextIndex
}
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
guard let current = self.viewControllers?.first else {
// TODO handle error
return
}
guard let index = self.pages.firstIndex(of: current) else {
// TODO handle error
return
}
self.currentIndex = index
}
guard
语句应该不会失败,但以防万一……对于我的情况,我能够通过使用func pageViewController(pageViewController:UIPageViewController,didffinishanimating finished:Bool,previousViewControllers:[AnyObject],transitionCompleted completed:Bool)来确定滑动方向
这里是如何。假设您已经有了一种方法来引用名为currentIndex
的当前索引。在上面的方法中,您已经有了关于previousViewControllers
的信息,因此可以获得名为previousIndex
的上一个索引
基本上,以下是委托方法内部的实现:
func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [AnyObject], transitionCompleted completed: Bool) {
if completed {
if previousIndex < currentIndex { // forward
}
else if previousIndex > currentIndex { // reverse
}
}
}
func pageViewController(pageViewController:UIPageViewController,didFinishAnimating finished:Bool,PreviousViewController:[AnyObject],transitionCompleted completed:Bool){
如果完成{
如果以前的索引<当前索引{//向前
}
如果上一个索引>当前索引{//反向,则为else
}
}
}
首先在viewControllerForIndex方法中指定viewcontroller.view.tag=索引,以便每个viewcontroller.view都有一个指定值
具有一个currentIndex属性,该属性最初分配给您在pageviewcontroller中实例化的viewcontroller的索引
现在在didFinishAnimating中,请执行以下操作
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if !completed {
return // return if animation is not completed
}
let pCont = bottomDataContainer?.viewControllers?[0] as? CustomVC //take
out current VC
if currentIndex < pCont?.view.tag ?? 0 {
topNavView.goToNextDate()
}else{
topNavView.goToPrevDate()
}
selectedIndex = pCont?.view.tag ?? 0
}
func pageViewController(pageViewController:UIPageViewController,didFinishAnimating finished:Bool,PreviousViewController:[UIViewController],transitionCompleted completed:Bool){
如果!完成{
return//动画未完成时返回
}
将pCont=bottomDataContainer?.ViewController?[0]设为?CustomVC//take
外流VC
如果currentIndex
非常感谢您准确回答我的问题。请您解释一下如何获取挂起的viewController的索引。您会说“…等于挂起的视图控制器的itemIndex”,但我对如何做到这一点没有任何线索。答案的其余部分很清楚,写得很好——再次感谢@我已经用一些示例代码更新了我的答案。我真是太感谢你了!我想补充一下。当您移动到下一个控制器并立即向相反方向刷回时,会出现这种情况。在这种情况下,“WillTransitionViewController”不会在最后一次刷卡时调用。因此您必须添加:如果(已完成){…}否则{nextIndex=previousViewController.index}我该怎么做?“您可以通过向ViewController添加属性来跟踪索引,然后可以比较页面索引。”。谢谢@CeceXX视图控制器将具有一个名为pageIndex的属性,如本教程所示: