如何让我的UIPageViewController自动滑动图像?
我已经创建并实现了所有内容,现在我希望它能够以10秒的时间间隔自动滑动图像......
任何人都可以帮我吗?
答案 0 :(得分:2)
在管理UIPageViewController的类中:
拥有像
这样的属性@property NSInteger currentIndex;
将其设置为0,每次更改为另一个viewController时,更新其值(_currentIndex ++ / _currentIndex - )
然后实现NSTimer:
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(changeController:)
userInfo:nil
repeats:YES];
并添加此方法
- (void) changeController:(NSTimer*)timer
{
UIViewController yourViewController = [self itemControllerForIndex: self.currentIndex+1];
self.currentIndex++;
NSArray *array = [NSArray arrayWithObjects:yourViewController]
[yourPageViewController setViewControllers:array direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
希望它有所帮助...
答案 1 :(得分:1)
每次要更改页面时,都需要调用- setViewControllers:direction:animated:completion:
方法。
您可以使用NSTimer
来启动您自己的中间方法,该方法使用上述方法设置相应的页面。
答案 2 :(得分:0)
这是我的代码:
- (UIViewController *) pageViewController: (UIPageViewController *) pageViewController viewControllerBeforeViewController:(UIViewController *) viewController
{
HelpPageViewController *itemController = (HelpPageViewController *) viewController;
if (itemController.itemIndex > 0)
{
return [self itemControllerForIndex: itemController.itemIndex-1]; // bilo je -1
}
return nil;
}
- (UIViewController *) pageViewController: (UIPageViewController *) pageViewController viewControllerAfterViewController:(UIViewController *) viewController
{
HelpPageViewController *itemController = (HelpPageViewController *) viewController;
if (itemController.itemIndex+1 < [contentImages count]) // bilo je <
{
return [self itemControllerForIndex: itemController.itemIndex+1];
}
return nil;
}
UIPageViewController *scrollingHeader = [self.storyboard instantiateViewControllerWithIdentifier: @"ScrollingHeader"];
scrollingHeader.dataSource = self;
if([contentImages count])
{
NSArray *startingViewControllers = @[[self itemControllerForIndex: 0]];
[scrollingHeader setViewControllers: startingViewControllers
direction: UIPageViewControllerNavigationDirectionForward
animated: NO
completion: nil];
}
self.pageViewController = scrollingHeader;
[self addChildViewController: self.pageViewController];
[self.view addSubview: self.pageViewController.view];
[self.pageViewController didMoveToParentViewController: self];