美文网首页
UIPageViewController的用法

UIPageViewController的用法

作者: myk | 来源:发表于2018-06-07 15:04 被阅读44次
  • 因为要做按钮和view的联动效果,所以决定使用UIPageViewController,当点击按钮的时候展示对应的view,滑动UIPageViewController切换到对应的按钮
初始化
//添加控制器
-(void)loadVcs{
    NewTaskController *newVc = [[NewTaskController alloc] init];
    PickUpController *pickVc = [[PickUpController alloc] init];
    DistributionController *disVc = [[DistributionController alloc] init];
    
    self.arrVcs = [NSArray arrayWithObjects:newVc, pickVc, disVc, nil];
    
    self.pageVc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    [self.pageVc setViewControllers:@[self.arrVcs[0]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    self.pageVc.delegate = self;
    self.pageVc.dataSource = self;
    self.pageVc.view.frame = CGRectMake(0, 40, ScreenWidth, ScreenHeight - 40);
    [self addChildViewController:self.pageVc];
    [self.pageVc didMoveToParentViewController:self];
    [self.view addSubview:self.pageVc.view];
}
点击按钮事件
#pragma mark ----按钮点击事件
-(void)btnAction:(UIButton *)sender{
    sender.selected = YES;
    NSInteger newSelectIndex = sender.tag - 888;
    if (oldSelectIndex == newSelectIndex) {
        
    }else{
        UIViewController *vc = self.arrVcs[newSelectIndex];
        if (oldSelectIndex > newSelectIndex) {
            [self.pageVc setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:^(BOOL finished) {
            }];
        }else{
            [self.pageVc setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
            }];
        }
        UIButton *btn_old = self.arrBtn[oldSelectIndex];
        btn_old.selected = NO;
        oldSelectIndex = newSelectIndex;
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomView.frame = CGRectMake(newSelectIndex * KBtnWidth, 40, KBtnWidth, 1);
        }];
    }
}
DataSource方法
#pragma mark 返回上一个ViewController对象
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    
    NSInteger nowIndex = [self.arrVcs indexOfObject:viewController];
    if (nowIndex == 0) {
        return nil;
    }else{
        return self.arrVcs[nowIndex - 1];
    }
}

#pragma mark 返回下一个ViewController对象
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    NSInteger nowIndex = [self.arrVcs indexOfObject:viewController];
    if (nowIndex == self.arrVcs.count - 1) {
        return nil;
    }else{
        return self.arrVcs[nowIndex + 1];
    }
}
代理方法
-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed{
    //previousViewControllers:上一个控制器
    if (!completed) {
        return;
    }
    NSLog(@"previousViewControllers == %@", previousViewControllers.lastObject);
    UIViewController *vc = previousViewControllers.firstObject;
    NSInteger previousIndex = [self.arrVcs indexOfObject:vc];
    if (previousIndex == currentIndex) {
        
    }else{
        UIButton *btn_current = self.arrBtn[currentIndex];
        UIButton *btn_old = self.arrBtn[previousIndex];
        btn_old.selected = NO;
        btn_current.selected = YES;
        
        oldSelectIndex = currentIndex;
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomView.frame = CGRectMake(currentIndex * KBtnWidth, 40, KBtnWidth, 1);
        }];
    }
}

-(void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers{
    //pendingViewControllers:下一个控制器
    NSLog(@"pendingViewControllers === %@", pendingViewControllers);
    currentIndex = [self.arrVcs indexOfObject:pendingViewControllers.firstObject];
}

相关文章

网友评论

      本文标题:UIPageViewController的用法

      本文链接:https://www.haomeiwen.com/subject/isxgsftx.html