Progress of UIPageViewController

后端 未结 7 1109
礼貌的吻别
礼貌的吻别 2020-12-30 05:51

I would like to receive updates from the uipageviewcontroller during the page scrolling process. I want to know the transitionProgress in %. (This value should update when t

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 06:11

    Since I thought that the functionality of scrolling would stay forever, but that the internal implementation may change to something other than a scroll view, I found the solution below (I haven't tested this very much, but still)

    NSUInteger offset = 0;
    UIViewController * firstVisibleViewController;
    while([(firstVisibleViewController = [self viewControllerForPage:offset]).view superview] == nil) {
      ++offset;
    }
    CGRect rect = [[firstVisibleViewController.view superview] convertRect:firstVisibleViewController.view.frame fromView:self.view];
    CGFloat absolutePosition = rect.origin.x / self.view.frame.size.width;
    absolutePosition += (CGFloat)offset;
    

    (self is the UIPageViewController here, and [-viewControllerForPage:] is a method that returns the view controller at the given page)

    If absolutePosition is 0.0f, then the first view controller is shown, if it's equal to 1.0f, the second one is shown, etc... This can be called repeatedly in a CADisplayLink along with the delegate methods and/or UIPanGestureRecognizer to effectively know the status of the current progress of the UIPageViewController.

    EDIT: Made it work for any number of view controllers

提交回复
热议问题