iOS:UIPageViewController - 使用按钮跳转到下一页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23835805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:12:08  来源:igfitidea点击:

iOS: UIPageViewController - Use button to jump to next page

iosuibuttonuipageviewcontroller

提问by MJN

I have a series of VCs in a PageViewController that a user navigates left to right through with their fingers. I need to add in buttons that essentially perform the same action as the finger swiping, moving left or right by one through the VCs . How can I do this? Right now I am using these two methods to dynamically setting the VCs as the user swipes:

我在 PageViewController 中有一系列 VC,用户可以用手指从左到右导航。我需要添加按钮,这些按钮基本上执行与手指滑动相同的操作,通过 VC 向左或向右移动一个。我怎样才能做到这一点?现在我正在使用这两种方法在用户滑动时动态设置 VC:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController          viewControllerBeforeViewController:(UIViewController *)viewController;

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;

Is there someway I can do the same thing if a user clicks a button?

如果用户单击按钮,我是否可以做同样的事情?

回答by MJN

You can programmatically set the currently displayed view controller with a transition animation using setViewControllers:direction:animated:completion:on your page view controller.

您可以使用setViewControllers:direction:animated:completion:页面视图控制器上的过渡动画以编程方式设置当前显示的视图控制器。

Here is an example that presents view controllers with random background colors. You can adjust this to use your specific view controllers.

这是一个展示具有随机背景颜色的视图控制器的示例。您可以调整它以使用您的特定视图控制器。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.pvc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    self.pvc.view.frame = CGRectInset(self.view.bounds, 200, 200);
    [self.view addSubview:self.pvc.view];

    [self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}

-(UIViewController*)randomVC
{
    UIViewController *vc = [[UIViewController alloc] init];
    UIColor *color = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
    vc.view.backgroundColor = color;
    return vc;
}

- (IBAction)previousButtonPressed:(id)sender {
    [self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
}

- (IBAction)nextButtonPressed:(id)sender {
    [self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}

回答by Yogesh Lolusare

In case Pages can be navigated by added button(Previous, Next) & also by swipewith page control ON. Please go through below:

如果页面可以通过添加的按钮(上一个,下一个)导航,也可以在页面控制打开的情况下滑动。请通过以下:

//Set Delegate & Data Source for PageView controller [Say in View Did Load]
   self.pageViewController.dataSource = self;
   self.pageViewController.delegate = self;



// PageBefore & After When User Scroll to move next or previous page 

 - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{

    [_nextBtn setTitle:@"Next" forState:UIControlStateNormal];

    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

    if (index == NSNotFound)
    {
        return nil;
    }

    if (index > 0)
    {
        index--;
    }
    else
    {
        return nil;
    }

    return [self viewControllerAtIndex:index];

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    NSUInteger index = ((PageContentViewController*) viewController).pageIndex;

if (index == NSNotFound) {
    return nil;
}


if (index < 3)
{
    index++;
}else
{
    return nil;
}
return [self viewControllerAtIndex:index];}

//To Match Exact Index page view when scrolled & navigated using button action. Place button index when page is been translated.



 - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers{    
buttonIndex = (int)((PageContentViewController*) pendingViewControllers.firstObject).pageIndex;}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{    
if (buttonIndex == 0)
{
    _backButton.hidden = true;
}else if (buttonIndex == [self.pageImages count] - 1)
{
    _backButton.hidden = false;
    [_nextBtn setTitle:@"Begin" forState:UIControlStateNormal];
}else
{
    _backButton.hidden = false;
}
}

//Previous , Next Button Actions

//上一个,下一个按钮动作

-(void)backBtnClicked:(id)sender{
if (buttonIndex > 0)
{
buttonIndex -= 1;
}
    if (buttonIndex < 1) {
        _backButton.hidden = YES;
    }
    if (buttonIndex >=0) {
         [_nextBtn setTitle:@"Next" forState:UIControlStateNormal];
        PageContentViewController *startingViewController = [self viewControllerAtIndex:buttonIndex];
        NSArray *viewControllers = @[startingViewController];
        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    }}

-(void)nextBtnAction:(id)sender{
if (buttonIndex < 3)
{
buttonIndex += 1;
}

if(buttonIndex == _pageImages.count){
   //Navigate Outside Pageview controller        
}   else{        
    if (buttonIndex ==3) {

        [_nextBtn setTitle:@"Begin" forState:UIControlStateNormal];
    }
    _backButton.hidden = NO;

    PageContentViewController *startingViewController = [self viewControllerAtIndex:buttonIndex];
    NSArray *viewControllers = @[startingViewController];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}}

//BUTTON INDEX
-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{
    return [self.pageImages count];}

-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{
    return  buttonIndex;}