这两种方法viewControllerBeforeViewController
以及viewControllerAfterViewController
的两种方法UIPageViewControllerDataSource
都不会告诉您滑动的方向。
该方法transitionCompleted
的的UIPageViewController
委托不帮助我们,所以尽管。它只是告诉页面是否被完全刷了。
那么我应该使用哪种方法来准确地检测用户方向(左或右)?
这两个属性可能会有所帮助:
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:
委托方法来确定转换是向前还是向后。在课程开始时声明几个索引变量,例如currentIndex
和nextIndex
(均为Int
)。
在该willTransitionToViewControllers
方法中,将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
}
}
结束编辑
您可以在此时确定转换是前进还是后退:如果nextIndex
> currentIndex
,则前进;如果nextIndex
<
currentIndex
则向后。然后在中didFinishAnimating
,如果completed
为true(因此它完成了向下一个视图控制器的转换),请将其设置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:)
方法中使用。
结束编辑
我已经实现了swipe以将其删除到我的RecycerView中。它触发2个功能(编辑记录和删除)。如果你向左滑动,你编辑它,如果向右滑动,你删除它。我在RecycerView项的一般部分下有这样的布局
我用Java SDK在HarmonyOS中编写了一个自定义组件,它是一个自定义页面滑块指示器。为此,我添加了一个,它提供了三个重写方法。 每当用户滑动页面时,将调用,这里我面临的问题是,和对于左右滑动是相同的。 那么,如何知道滑动的方向呢?
基本上,我正在使用TestNG开发Selenium automation,我想为一个字段获取用户输入,而要获取用户输入,我需要scanner类的帮助。但如果没有主要阶层,它就不起作用。有人能帮我吗?
我想在查看页面2中禁用从右向左滑动。我基本上有一个viewpager2元素,在导航抽屉中有两页。我希望我的第二页仅在单击第一页中的某个元素时显示(从第一页向右向左滑动不应打开第二页),而当我在第二页时,viewpager2滑动(从左向右滑动)应像在viewpager中一样滑动。 我尝试过扩展类并覆盖触摸事件,但不幸的是它是最终类,因此我无法扩展它。 其次,我尝试使用将userinputened设置
问题内容: 如何获取Java中的用户名/登录名? 这是我尝试过的代码… 我在尝试运行此代码时得到提示。有人可以告诉我我是否朝着正确的方向前进,并帮助我理解问题。 问题答案: