gpt4 book ai didi

ios - 在 UIPageViewController 中获取用户滑动方向

转载 作者:IT王子 更新时间:2023-10-29 05:39:33 26 4
gpt4 key购买 nike

UIPageViewControllerDataSource 的这两个方法 viewControllerBeforeViewControllerviewControllerAfterViewController 不会告诉滑动的方向。

UIPageViewController 委托(delegate)的方法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)
}

}
}
}

根据pbasdf所说的,

var currentIndex: Int = 0
var nextIndex: Int = 0

func pageViewController(PageItemController: UIPageViewController,
willTransitionToViewControllers pageViewController: [AnyObject]) {

// pageViewController is the pending View Controller
nextIndex = currentIndex

// pageViewController...... how do I get its index?
}

最佳答案

您应该同时使用 willTransitionToViewControllers:didFinishAnimating: 委托(delegate)方法来确定过渡是向前还是向后。在类的开头声明几个索引变量,例如 currentIndexnextIndex(均为 Int)。

willTransitionToViewControllers 方法中,将 nextIndex 设置为挂起 View Controller 的 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
}
}

结束编辑

此时您可以确定转换是向前还是向后:如果 nextIndex > currentIndex,则向前;如果 nextIndex <currentIndex 则向后。然后在 didFinishAnimating 中,如果 completed 为真(所以它完成了到下一个 View Controller 的转换),设置 currentIndex 等于 nextIndex 这样您就可以在任何需要指示屏幕上当前显示哪个页面的地方使用 currentIndex:

编辑

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(_, direction:) 方法中使用。

结束编辑

关于ios - 在 UIPageViewController 中获取用户滑动方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27931961/

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