iOS: UIPageViewController - Use button to jump to next page

我的未来我决定 提交于 2019-12-04 22:00:28

问题


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:

- (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?


回答1:


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

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];
}



回答2:


In case Pages can be navigated by added button (Previous, Next) & also by swipe with 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;}


来源:https://stackoverflow.com/questions/23835805/ios-uipageviewcontroller-use-button-to-jump-to-next-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!