UIPageViewController: Fade In/Out Animation Between Viewcontrollers?

拥有回忆 提交于 2019-12-02 03:16:24

Use the delegate pageViewController:willTransitionToViewControllers:. This is called as the gesture begins. You know what the old view controller is because it's already there. You know what the new view controller is because this message tells you. Thus you have access to their views. Thus you can change the alpha of their views.

The gesture may be cancelled, but then pageViewController:didFinishAnimating:previousViewControllers:transitionCompleted: tells you this, so you can fix the alpha of the existing view back again.

If you don't like that approach, you're going to have to subclass UIPageViewController, or write your own parent view controller that does the same sort of thing. The advantage there is that you have direct access to the scroll view and can track the scroll yourself. Writing an "infinite scroll view" is discussed in a WWDC video from some years ago; I've written one, but I abandoned it when UIPageViewController took on the scrolling style.

Using the suggestion from @matt, heres how I ended up implementing it:

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *>
*)pendingViewControllers {

    _nextVC = [pendingViewControllers objectAtIndex:0];

}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(nonnull NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (completed)
    {
        [_welcomeBackground setImage:[NSString stringWithFormat:@"welcome_intro_image_%d", _nextVC.view.tag]] 
    } 
}

We check for completed to make sure the user actually moved on to the next page, then set the correct image for that page according _nextVC's index which is stored in its tag property.

Use the custom SCPageViewController and fade the view in the pageViewController:didNavigateToOffset: delegate method

This is included in the default transition styles in interface builder

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