ios - UIPageViewController 在 iOS 中自动滚动
<p><p>我正在制作一个应用程序,其中 PageViewController 应该在某个时间间隔内滚动。目前我已经完成了手动滚动。用户可以滚动任何图像,它会起作用。但坚持通过用户触摸实现自动滚动。下面是我的代码。请帮我解决这个问题。</p>
<pre><code>- (void)viewDidLoad {
;
// Do any additional setup after loading the view.
_pageTitles = @[@"", @"", @"", @""];
_pageImages = @[@"page1.png", @"page2.png", @"page3.png", @"page4.png"];
// Create page view controller
self.pageViewController = ;
self.pageViewController.dataSource = self;
PageContentViewController *startingViewController = ;
NSArray *viewControllers = @;
;
// Change the size of page view controller
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50);
;
;
;
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index
{
if (( == 0) || (index >= )) {
return nil;
}
// Create a new view controller and pass suitable data.
PageContentViewController *pageContentViewController = ;
pageContentViewController.imageFile = self.pageImages;
pageContentViewController.titleText = self.pageTitles;
pageContentViewController.pageIndex = index;
return pageContentViewController;
}
-(void)viewWillAppear:(BOOL)animated {
;
self.navigationController.navigationBar.hidden = YES;
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return ;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == ) {
return nil;
}
return ;
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return ;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>@Daniel 您的链接已损坏,您的“答案”几乎无法解释您对问题的解决方案。对于那些有兴趣阅读 <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPageViewControllerClassReferenceClassRef/" rel="noreferrer noopener nofollow">UIPageViewController Class</a> 文档的人.</p>
<p>这里要注意的方法是:</p>
<pre><code>- (void)setViewControllers:(NSArray *)viewControllers
direction:(UIPageViewControllerNavigationDirection)direction
animated:(BOOL)animated
completion:(void (^)(BOOL finished))completion
</code></pre>
<p>UIPageViewController 希望您根据值告诉它要传送什么数据以及使用什么 Controller 。在这里,我告诉我的 UIPageController 子类是委托(delegate)和 dataSouce。配置委托(delegate)方法和 viewControllerAtIndex dataSource 方法后,我添加了一个调用我的 loadNextController 方法的自定义间隔。在那种方法中,我正在检查“下一个” Controller 是否为零。如果它不是零,我将“下一个” Controller 作为@ 传递回对 setViewControllers 的第二次调用。如果它是零,我只是将索引值重置为零,并使用我的“下一个” Controller 调用 setViewControllers 作为@[起始 Controller ]。此自定义子类用于自动分页到下一个或第一个 Controller 。好处是用户交互仍然由 UIPageViewController 类处理。 </p>
<p>注意:需要对 setViewControllers 进行初始调用,因为您的 UIPageViewController 将不知道是否可以先加载任何 viewController。我第二次调用 setViewControllers 只是跳到下一个或第一个 viewController。 </p>
<pre><code>- (void)viewDidLoad {
;
self.index = 0;
self.delegate = self;
self.dataSource = self;
/*! setup an interval */
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(loadNextController)
userInfo:nil
repeats:YES];
/*! set starting controller */
NSArray *startingViewControllers = @[];
[self setViewControllers: startingViewControllers
direction: UIPageViewControllerNavigationDirectionForward
animated: YES
completion: nil];
}
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger index = ((YSPageContentViewController*) viewController).pageIndex;
if (index > 0 ) {
return ;
}
return nil;
}
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger index = ((YSPageContentViewController*) viewController).pageIndex;
if (index+1 < ) {
return ;
}
return nil;
}
- (PageContentViewController *)viewControllerAtIndex:(NSUInteger)index{
if (index < ) {
PageContentViewController *pageContentViewController = [init];
pageContentViewController.pageIndex = index;
pageContentViewController.image = (UIImage*)self.items;
return pageContentViewController;
}
return nil;
}
/*! set next controller */
- (void)loadNextController {
PageContentViewController *nextViewController = ;
if (nextViewController == nil) {
self.index = 0;
nextViewController = ;
}
direction:UIPageViewControllerNavigationDirectionForward
animated:YES
completion:nil];
}
</code></pre>
<p>注意:这确实有效;但是,当内部手势识别器更改 Controller 时,我并不总是返回正确的 Controller 。一路上的某个地方我失去了对索引的跟踪。我将用我完成的实现更新它。 </p></p>
<p style="font-size: 20px;">关于ios - UIPageViewController 在 iOS 中自动滚动,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/26668536/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/26668536/
</a>
</p>
页:
[1]