gpt4 book ai didi

ios - 半透明模态 ViewController - 如何处理旋转

转载 作者:可可西里 更新时间:2023-11-01 04:51:56 24 4
gpt4 key购买 nike

我想以模态方式显示 UIViewController 并且能够看到呈现它的 View 的模糊版本。

下面是一些类似的问题,例如:

iOS 7 Translucent Modal View Controller

我已将背景添加到我的 Controller View 中,该 View 基于呈现 Controller 的捕获 View 。我面临的问题是我的应用程序支持多个方向,当呈现和旋转模态视图时,底层背景图像不再匹配。

我尝试在模态视图 Controller 的 didRotateFromInterfaceOrientation: 中获取呈现 View Controller 的新快照,但呈现 View Controller 的 UI 似乎没有更新,生成的图像仍然是错误的方向。有什么方法可以强制重绘被模态视图隐藏的 View 吗?

最佳答案

经过长时间的考虑,我想出了一个可以接受的处理方式。它的工作效果在一定程度上取决于您在呈现 viewController 中的内容类型。

总体思路是在呈现新的 viewController 之前截取两张屏幕截图,一张用于纵向,一张用于横向。这是通过更改顶部 viewController 和导航栏(如果适用)的框架以模拟不同的方向,获取结果的屏幕截图并将其更改回来来实现的。用户永远不会在设备上看到这种变化,但屏幕抓取仍然显示新的方向。

确切的代码将取决于您从何处调用它,但主要逻辑是相同的。我的实现从 AppDelegate 运行,因为它被 UIViewController 的几个子类重用。

以下是获取相应屏幕截图的代码。

// get references to the views you need a screenshot of
// this may very depending on your app hierarchy
UIView *container = [self.window.subviews lastObject]; // UILayoutContainerView
UIView *subview = container.subviews[0]; // UINavigationTransitionView
UIView *navbar = container.subviews[1]; // UINavigationBar

CGSize originalSubviewSize = subview.frame.size;
CGSize originalNavbarSize = navbar.frame.size;

// compose the current view of the navbar and subview
UIImage *currentComposed = [self composeForeground:navbar withBackground:subview];

// rotate the navbar and subview
subview.frame = CGRectMake(subview.frame.origin.x, subview.frame.origin.y, originalSubviewSize.height, originalSubviewSize.width);
// the navbar has to match the width of the subview, height remains the same
navbar.frame = CGRectMake(navbar.frame.origin.x, navbar.frame.origin.y, originalSubviewSize.height, originalNavbarSize.height);

// compose the rotated view
UIImage *rotatedComposed = [self composeForeground:navbar withBackground:subview];

// change the frames back to normal
subview.frame = CGRectMake(subview.frame.origin.x, subview.frame.origin.y, originalSubviewSize.width, originalSubviewSize.height);
navbar.frame = CGRectMake(navbar.frame.origin.x, navbar.frame.origin.y, originalNavbarSize.width, originalNavbarSize.height);


// assign the variables depending on actual orientations
UIImage *landscape; UIImage *portrait;
if (originalSubviewSize.height > originalSubviewSize.width) {
// current orientation is portrait
portrait = currentComposed;
landscape = rotatedComposed;
} else {
// current orientation is landscape
portrait = rotatedComposed;
landscape = currentComposed;
}
CustomTranslucentViewController *vc = [CustomTranslucentViewController new];
vc.backgroundSnap = portrait;
vc.backgroundSnapLandscape = landscape;
[rooVC presentViewController:vc animated:YES completion:nil];

composeForeground:withBackground: 方法是一种基于两个输入 View (导航栏 + View Controller )生成适当背景图像的便捷方法。除了将两个 View 组合在一起之外,在旋转呈现的 viewController 时,它还可以使结果看起来更自然。具体来说,它将屏幕截图扩展到 1024x1024 正方形,并用合成图像的镜像副本填充额外空间。在许多情况下,一旦模糊,这看起来就足够了,因为针对方向更改重新绘制 View 的动画不可用。

- (UIImage *)composeForeground:(UIView *)frontView withBackground:(UIView *)backView {

UIGraphicsBeginImageContextWithOptions(backView.frame.size, 0, 0);
[backView.layer renderInContext:UIGraphicsGetCurrentContext()];

// translation is necessary to account for the extra 20 taken up by the status bar
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), frontView.frame.origin.x, frontView.frame.origin.y);
[frontView.layer renderInContext:UIGraphicsGetCurrentContext()];
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -frontView.frame.origin.x, -frontView.frame.origin.y);

// this is the core image, would have left it at this if we did not need to use fancy mirrored tiling
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// add mirrored sections
CGFloat addition = 256; // 1024 - 768
if (newImage.size.height > newImage.size.width) {
// portrait, add a mirrored image on the right
UIImage *horizMirror = [[UIImage alloc] initWithCGImage:newImage.CGImage scale:newImage.scale orientation:UIImageOrientationUpMirrored];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(newImage.size.width+addition, newImage.size.height), 0, 0);
[horizMirror drawAtPoint:CGPointMake(newImage.size.width, 0)];
} else {
// landscape, add a mirrored image at the bottom
UIImage *vertMirror = [[UIImage alloc] initWithCGImage:newImage.CGImage scale:newImage.scale orientation:UIImageOrientationDownMirrored];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(newImage.size.width, newImage.size.height+addition), 0, 0);
[vertMirror drawAtPoint:CGPointMake(0, newImage.size.height)];
}

// combine the mirrored extension with the original image
[newImage drawAtPoint:CGPointZero];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// for ios 6, crop off the top 20px
if (SYSTEM_VERSION_LESS_THAN(@"7")) {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(newImage.size.width, newImage.size.height-20), NO, 0);
[newImage drawAtPoint:CGPointMake(0, -20)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}

return newImage;
}

生成的横向和纵向图像可以根据需要进行模糊和着色,并设置为呈现的 viewController 的背景。使用此 viewControllerwillRotateToInterfaceOrientation:duration: 方法选择适当的图像。

注意:我已尝试尽可能减少对图像和图形上下文所做的工作量,但在生成背景时仍然存在轻微延迟(大约 30-90 毫秒/composeForeground:withBackground: 迭代,取决于内容,在老式慢速 iPad 2 上。如果您知道进一步优化或简化上述解决方案的方法,请分享!

关于ios - 半透明模态 ViewController - 如何处理旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22413606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com