UIPageViewController delegate method similar to scrollViewDidScroll:(UIScrollview*)scrollview

夙愿已清 提交于 2019-12-24 04:41:26

问题


I'm using a UIPageViewController to swipe through a series of viewcontrollers. I'd like the background of the parent viewcontroller to smoothly transition between colors when swiping, based on the position of the scroll between views. I was hoping UIPageViewController would have a delegate method similar to scrollViewDidScroll with the position, but it doesn't seem to.

Is there an alternate method or similar method to expose the position of the swipe as it occurs/changes?

EDIT: Since scrollview seems not to be exposed, I've come up with a perm/temp solution. When transitioning between pages I transition the parent background color over an Animation of 1 second. This does not directly tie to the swipe, or allow for half swipes, etc. But for regular side to side swipes it accomplishes the desired smooth effect instead of instant color change.

// code that occurs when a pageControl swipe occurs. 

UIColor *destinationColor = [UIColor redColor];  // the desired end color

[UIView animateWithDuration:1.0
                      delay:0
                    options:UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     self.pageViewController.view.backgroundColor = destinationColor;
                 }
                 completion:nil
];

回答1:


While the UIScrollView of the UIPageViewController isn't exposed by default, it's actually not that hard to find, so you can do what you want by making your view controller an UIScrollViewDelegate and then doing something like this in the viewDidLoad method or the like:

for (UIView *view in pageViewController.view.subviews) {
    if ([view isKindOfClass:[UIScrollView class]]) {
        ((UIScrollView *)view).delegate = self;
        break;
    }
}

Now your view controller can implement scrollViewDidScroll: and be able to track the transition of the page view controller.




回答2:


If you mean you want the transition between the two view controllers to track the user's finger as they swipe, then set the datasource and implement the methods: viewControllerBeforeController and viewControllerAfterController. If these are implemented, UIPageViewController will automatically implement a gesture recogniser which tracks the user's swipe and animates the transition between the two viewControllers accordingly.



来源:https://stackoverflow.com/questions/25983049/uipageviewcontroller-delegate-method-similar-to-scrollviewdidscrolluiscrollvie

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