问题描述
这两个方法 viewControllerBeforeViewController
和 viewControllerAfterViewController
的 UIPageViewControllerDataSource
不要告诉滑动的方向。
These two methods viewControllerBeforeViewController
and viewControllerAfterViewController
of the UIPageViewControllerDataSource
don't tell the direction of the swipe.
的方法
委托对我们也没什么帮助。它只是告诉页面是否完全刷过。 transitionCompleted
UIPageViewController
The method transitionCompleted
of the UIPageViewController
delegate doesn't help us much either. It just tells if the page was fully swiped.
那么我应该使用什么方法来准确检测用户方向(左或右)?
So what method should I use to detect exactly the user direction (left or right)?
这两个属性可能会有所帮助:
Probably these two properties may help:
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的说法,
According to what pbasdf said,
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:
委托方法来确定过渡是向前还是向后。在类的开头声明几个索引变量,例如 currentIndex
和 nextIndex
(两者都是 Int
)。
You should use both the willTransitionToViewControllers:
and the didFinishAnimating:
delegate methods to work out whether the transition is forward or backward. Declare a couple of index variables at the start of your class, say currentIndex
and nextIndex
(both Int
).
在 willTransitionToViewControllers
方法中,设置 nextIndex
等于挂起视图控制器的 itemIndex
:
In the willTransitionToViewControllers
method, set the nextIndex
equal to the itemIndex
of the pending view controller:
编辑
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
,然后转发; if nextIndex
< currentIndex
然后向后。然后在 didFinishAnimating
中,如果已完成
为真(因此它完成了向下一个视图控制器的转换),请设置 currentIndex
等于 nextIndex
所以你可以在任何需要的地方使用 currentIndex
指示当前屏幕上的哪个页面:
You can work out at this point whether the transition will be forward or backward: if nextIndex
> currentIndex
, then forward; if nextIndex
< currentIndex
then backward. Then in didFinishAnimating
, if completed
is true (so it completed the transition to the next view controller), set currentIndex
equal to nextIndex
so you can use currentIndex
wherever you need to indicate which page is currently on screen:
编辑
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(UIPa的实例) geViewController),而不是当前代码中的 pageItemController
。
Note that the first argument to these methods is the pageViewController (instance of UIPageViewController), not pageItemController
which you have in your current code.
最后,请注意:枚举你引用( UIPageViewControllerNavigationDirection
)仅用于 setViewControllers(_,direction:)
方法。
Finally, just to note: that enum you refer to (UIPageViewControllerNavigationDirection
) is used only in the setViewControllers(_, direction:)
method.
结束编辑
这篇关于在UIPageViewController中获取用户滑动方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!