是否可以在 UIPageViewController 中以编程方式翻页?
是否可以在 UIPageViewController 中以编程方式翻页?
Is it possible to turn page programmatically in UIPageViewController
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
是的,可以使用以下方法:
这与用于在页面上设置第一个视图控制器的方法相同。类似的,你可以用它去其他页面。
想知道为什么 viewControllers 是一个数组,而不是单个视图控制器?
这是因为页面视图控制器可能有一个“书脊”(就像 iBooks 中的那样),一次显示 2 页内容。如果一次显示 1 页内容,则只需传入 1 元素数组即可。
Swift 中的一个例子:
Yes it is possible with the method:
That is the same method used for setting up the first view controller on the page. Similar, you can use it to go to other pages.
Wonder why
viewControllers
is an array, and not a single view controller?That's because a page view controller could have a "spine" (like in iBooks), displaying 2 pages of content at a time. If you display 1 page of content at a time, then just pass in a 1-element array.
An example in Swift:
由于我也需要这个,因此我将更详细地介绍如何执行此操作。
注意:我假设您使用标准模板表单来生成
UIPageViewController
结构 - 该结构在调用它时同时创建了modelViewController
和dataViewController
。如果您不明白我写的内容 - 返回并创建一个使用UIPageViewController
作为基础的新项目。到时候你就会明白了。因此,需要翻转到特定页面涉及设置上面列出的方法的各个部分。对于本练习,我假设它是一个显示两个视图的横向视图。此外,我将其实现为 IBAction,以便可以通过按下按钮或其他方式来完成 - 就像很容易使其选择器调用并传入所需的项目。
因此,对于这个示例,您需要显示两个视图控制器 - 并且可以选择,无论您是在书中前进还是后退。
请注意,我只是硬编码了前往第 4 页和第 4 页的位置。 5、使用前滑。
从这里您可以看到,您需要做的就是传入将帮助您获取这些项目的变量......
Since I needed this as well, I'll go into more detail on how to do this.
Note: I assume you used the standard template form for generating your
UIPageViewController
structure - which has both themodelViewController
anddataViewController
created when you invoke it. If you don't understand what I wrote - go back and create a new project that uses theUIPageViewController
as it's basis. You'll understand then.So, needing to flip to a particular page involves setting up the various pieces of the method listed above. For this exercise, I'm assuming that it's a landscape view with two views showing. Also, I implemented this as an IBAction so that it could be done from a button press or what not - it's just as easy to make it selector call and pass in the items needed.
So, for this example you need the two view controllers that will be displayed - and optionally, whether you're going forward in the book or backwards.
Note that I merely hard-coded where to go to pages 4 & 5 and use a forward slip.
From here you can see that all you need to do is pass in the variables that will help you get these items...
这是单页视图的另一个代码示例。这里省略了 viewControllerAtIndex 的实现,它应该返回给定索引的正确视图控制器。
可以通过调用更改页面
Here's another code example for a single paged view. Implementation for viewControllerAtIndex is omitted here, it should return the correct view controller for the given index.
Pages can be changed by calling
我可能在这里完全遗漏了一些东西,但这个解决方案似乎比许多其他提出的解决方案简单得多。
I could totally be missing something here, but this solution seems a lot simpler than many others proposed.
这段代码对我来说效果很好,谢谢。
这就是我用它所做的。一些用于前进或后退的方法,以及一种用于直接进入特定页面的方法。它适用于纵向视图的 6 页文档。如果将其粘贴到 pageViewController 模板的 RootController 的实现中,它将正常工作。
This code worked nicely for me, thanks.
This is what i did with it. Some methods for stepping forward or backwards and one for going directly to a particular page. Its for a 6 page document in portrait view. It will work ok if you paste it into the implementation of the RootController of the pageViewController template.
Swift 4:
当我点击 pagecontroller 视图控制器上的“下一步”时,我需要转到下一个控制器,并更新 pageControl 索引,所以这对我来说是最好、最直接的解决方案嗯>:
Swift 4:
I needed to go to the next controller when i tap next on a pagecontroller view controller and also update the pageControl index, so this was the best and most straightforward solution for me:
使用 dataSource 中的方法怎么样?
类推向后。
What about using methods from dataSource?
Analogically for backward.
但是,使用此“self.pageViewController setViewControllers”方法调用不会在已关闭页面的 viewController 上调用“viewDidDissapear”,而手动翻页确实会在已关闭页面上调用 viewDidDissapear。我相信这是我崩溃的原因
“提供的视图控制器数量 (0) 与请求的转换所需的数量 (1) 不匹配”
However using this 'self.pageViewController setViewControllers' method call does not call 'viewDidDissapear' on the viewController for the page that has been turned away, whereas manually turning a page does call viewDidDissapear on the turned away page. I believe this is the cause of my crash
"The number of view controllers provided (0) doesn't match the number required (1) for the requested transition"
在Swift中:
In Swift:
我还需要一个按钮来在 PageViewController 上向左导航,该按钮应该完全按照您对滑动动作的期望进行操作。
由于我在“pageViewController:viewControllerBeforeViewController”中已经制定了一些规则来处理自然的滑动导航,因此我希望按钮能够重用所有这些代码,但根本无法承担像前面的答案一样使用特定索引来访问页面。所以,我不得不采取替代解决方案。
请注意,以下代码适用于页面视图控制器,该控制器是我的自定义 ViewController 中的一个属性,并且其脊柱设置为 mid。
以下是我为“向左导航”按钮编写的代码:
您可以执行非常类似的操作来从此处创建向右导航按钮。
I also needed a button to navigate left on a PageViewController, one that should do exactly what you would expect from the swipe motion.
Since I had a few rules already in place inside "pageViewController:viewControllerBeforeViewController" for dealing with the natural swipe navigation, I wanted the button to re-use all that code, and simply could not afford to use specific indexes to reach pages as the previous answers did. So, I had to take an alternate solution.
Please note that the following code is for a Page View Controller that is a property inside my custom ViewController, and has its spine set to mid.
Here is the code I wrote for my Navigate Left button:
You can do something very similar to make a right navigation button from here.
如果 PAGE CONTROL 指示当前页是 O &&您希望页面通过滚动和滚动来导航也可以通过单击 Page ViewController 中的自定义按钮,下面可能会有所帮助。
// 当用户滚动到下一页时,调用 viewControllerAfterViewController
}
}
// 自定义按钮的(上一页和下一页)逻辑
}
}
In case if PAGE CONTROL to indicate current page is O && You want pages to navigate via scroll & also by clicking custom buttons in Page ViewController, below may be helpful.
// Delegate called viewControllerAfterViewController when User Scroll to next page
}
}
//Custom button's (Prev & next) Logic
}
}
//现在调用这个方法
[self moveToPage];
我希望它有效:)
//now call this method
[self moveToPage];
I hope it works :)
对于单页,我刚刚编辑了@Jack Humphries 的答案
For single page, I just edited the answer of @Jack Humphries
正如@samwize 指出的,分页的方式是使用
以下是我的做法:
我的数据源和委托是分开的。您可以调用此方法并且仅更改“下一个”视图。以前您必须应用分页规则。
也就是说,你必须检查方向和下一个控制器。如果您将该方法与自定义数据源一起使用,则您显示的控制器数组不会更改(因为您将在 NSObject 子类上使用自定义数组)。
使用您自己的数据源,该方法只需设置一个新视图(具有特定方向)。因为您仍然可以控制正常滑动时显示的页面。
As @samwize pointed out, the way of paginating is by using
Here's the way I did it:
I have my datasource and the delegate separate. You can call this method and only change the "next" view. Previously you have to apply the paging rules.
That is, you have to check direction and the next controller. If you use that method with your custom datasource the array of controllers that you're displaying won't change (since you'll use a custom array on a NSObject subclass).
Using your own datasource the method just sets a new view (with specific direction). Since you will still controll the pages that will display when normally swiping.
我从昨天开始就在努力,终于成功了。我不能完全使用标准模板,因为我有三个不同的 viewController 作为子视图:
我只需要在第一个 VC 中触发按钮,所以我为 pageViewController 和模型添加了一个属性:
我重写了模型的 init 方法来传递 Storyboard 和pageViewController 作为参数,因此我可以将它们设置到我的 VC1:
最后,我在 vc1 中添加了一个 IBAction 来触发 pageViewController 方法setViewControllers:direction:animated:completion::
注意我不需要查找 VC 索引,我的按钮会将我带到第二个 VC,但获取所有索引并跟踪您的子视图并不难:
我希望它对您有所帮助!
I'm working on It since yesterday and I finally did It work. I couldn't use standard template totally, because I have three differents viewControllers as childviews:
I needed a trigger button only in the first one VC, so I added one property for pageViewController and model:
I rewrote the model's init method to pass storyboard and pageViewController as parameters, so I could set those to my VC1:
Finally, I added an IBAction in my vc1 to trigger the pageViewController method setViewControllers:direction:animated:completion::
Note I don't need to find VCs indexes, my button takes me to my second VC, but Its not hard get all of indexes, and keep track of your childviews:
I hope It helps you!
这是使用 UIPageViewControllerDataSource 方法的解决方案:
代码跟踪 UIPageViewController 中当前显示的视图控制器。
首先将currentViewController初始化为UIPageViewController设置的初始视图控制器。
然后在 UIPageViewControllerDelegate 方法中跟踪 currentViewController:pageViewController: willTransitionToViewControllers: 和 pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted: >。
最后,在您选择的方法中(在下面的示例中为 - (IBAction) onNext: (id) sender),您可以使用 UIPageViewControllerDataSouce 以编程方式切换到下一个或上一个控制器strong> 方法 pageViewController:viewControllerBeforeViewController: 、 pageViewController:viewControllerAfterViewController: 获取下一个或上一个视图控制器并通过调用导航到它[self.pageViewController setViewControllers: @[vc] 方向: UIPageViewControllerNavigationDirectionForward 动画: YES 完成: nil];
Here is a solution using UIPageViewControllerDataSource methods:
The code keeps track of the current displayed view controller in the UIPageViewController.
First initialize currentViewController to the initial view controller set for the UIPageViewController.
Then keep track of currentViewController in the UIPageViewControllerDelegate methods: pageViewController: willTransitionToViewControllers: and pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted:.
Finally, in the method of your choice (in the example below it's - (IBAction) onNext: (id) sender), you can programmatically switch to the next or previous controller using UIPageViewControllerDataSouce methods pageViewController:viewControllerBeforeViewController: , pageViewController:viewControllerAfterViewController: to get the next or prev view controller and navigate to it by calling [self.pageViewController setViewControllers: @[vc] direction: UIPageViewControllerNavigationDirectionForward animated: YES completion: nil];
由于 ViewController 嵌入在 PageViewController 中,您所要做的就是:
Xamarin 中的示例,但功能类似于 Swift 等。以下代码将位于显示为页面的 ViewController 之一的 Button Click 委托中:
As the ViewControllers are embedded in the PageViewController all you have to do is:
Example in Xamarin, but functionally similar to Swift etc. Following code would be in Button Click delegate in one of the ViewControllers being shown as a page:
最简单的 2022 解决方案:
无论如何,您都需要这个简单的功能:
然后为任何给定页面添加动画很容易:
就是这样。
Simplest 2022 solution:
No matter what, you need this simple function:
then animating to any give page is easy:
That's it.