How to change shadow background white to other colors

左心房为你撑大大i 提交于 2019-11-30 09:27:45
- (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx {
UIImage *image = [images objectAtIndex:index];
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
CGAffineTransform transform = aspectFit(imageRect,
                                            CGContextGetClipBoundingBox(ctx));
CGContextConcatCTM(ctx, transform);
CGContextDrawImage(ctx, imageRect, [image CGImage]);
}

I've been trying to do the same thing for some time now and I finally figured it out. Turns out you can't set the background color to other colors, but you can provide another view that UIPageViewController will add to the back. And that's the secret here.

From Apple's documentation:

Spine location                                Double sided               What to pass
UIPageViewControllerSpineLocationMid               YES               Left and Right Page.
SpineLocationMin or SpineLocationMax               YES               Front and Back of the page.
SpineLocationMin or SpineLocationMax               NO                Front page only.

So, basically, you need to set double sided property to yes and provide two viewControllers on both Data Source methods:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;

Each method will be called twice for every new page you add. So, you need to provide the viewController you would normally return and also a new viewController that will be added to the back when doing the page turning animation.

It's up to you what your "BackViewController" will have. You can simply have a black view or if you want, you can take a screenshot of the front page, and get a mirrored image from it.

It's not hard once you understand how it works. The only problem I can see here is that when you add a black view or anything that has a black background, the shadow when turning the page becomes WHITE. I have no idea why, but I've seen this happening on a lot of different apps, so I guess to Apple this is the normal behavior. But it looks really weird.

EDIT:

I've added a sample code so it is easier to understand. https://github.com/mattabras/DoubleSidedPageViewController

Abras

In the case of pagecontrol selectable page visibility color--

UIPageControl does not support this by default. You have to create your own page control and do the drawing on your own.

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