gpt4 book ai didi

ios - 如何使用主窗口不旋转但其余窗口旋转的单 View 应用程序模板创建应用程序?

转载 作者:行者123 更新时间:2023-11-29 11:47:46 24 4
gpt4 key购买 nike

如何使用主窗口不旋转但其 rootViewController 和其他所有内容自动旋转的单 View 应用模板创建应用?

Apple 在 CIFunHouse 上这样做但是因为代码在这方面的解释很差,所以不可能知道他们是怎么做到的。如果您运行该应用程序,您会看到相机的预览窗口不会自动旋转,因为预览已添加到窗口,但其他所有内容都会自动旋转。

Apple 在其原生 iPad 相机应用程序中使用了此技术。

最佳答案

所以答案不是很清楚,但我可以给你一个修复。在某些时候,主应用程序窗口不能像现在这样自动旋转,但在某些时候它开始根据 rootviewcontroller 旋转。至少这个源代码表明了这一点。我在 iOS 6 末期开始开发,我认为这个源代码是在那个时候写的。我能找到的允许在示例中旋转所有内容但预览不旋转的最佳修复是添加第二个窗口。将主窗口背景设置为清除。然后将 previewLayer 添加到主窗口后面的第二个窗口。在代码中它看起来像这样。

AppDelegate 看起来像这样。

#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>

@interface FHAppDelegate : UIResponder <UIApplicationDelegate>
{
@private
CMMotionManager *_motionManager;
}
@property (strong, readonly, nonatomic) CMMotionManager *motionManager;
//future preview window
@property (strong, nonatomic) UIWindow *previewWindow;
@property (assign, readonly, nonatomic) UIDeviceOrientation realDeviceOrientation;

@end

然后在 FHViewController 的 viewDidLoad 中,我没有添加到主窗口,而是添加了主窗口,我在其中添加了 previewView。

   // we make our video preview view a subview of the window, and send it to the back; this makes FHViewController's view (and its UI elements) on top of the video preview, and also makes video preview unaffected by device rotation
//nothing special about this viewcontorller except it has
//-(BOOL)shouldAutorotate{
//return NO;
//}

TestViewController *test = [[TestViewController alloc]initWithNibName:@"TestViewController" bundle:nil];
FHAppDelegate *delegate = ((FHAppDelegate *)[UIApplication sharedApplication].delegate);
delegate.previewWindow = [[UIWindow alloc]initWithFrame:window.bounds];
UIWindow *previewWindow = delegate.previewWindow;
[window setBackgroundColor:[UIColor clearColor]];

previewWindow.rootViewController = test;
previewWindow.windowLevel = UIWindowLevelNormal - 1;

[previewWindow setBounds:delegate.window.bounds];

[previewWindow makeKeyAndVisible];
[previewWindow addSubview:_videoPreviewView];
[previewWindow sendSubviewToBack:_videoPreviewView];

因为 previewWindow 必须有一个 rootviewcontroller 并且它决定了旋转你可以看到我的 testviewcontroller 的自动旋转为 NO。希望这可以帮助。它适用于 iOS 10。

编辑:上面示例中的 View 不旋转,但旋转时的窗口动画在视觉上很糟糕。它可以通过重写来删除willTransitionToSize

    [UIView setAnimationsEnabled:NO];

完成后

[UIView setAnimationsEnabled:YES];

请参阅 GitHub 上的 swift 版本

对于 iPad 需要全屏必须勾选。

关于ios - 如何使用主窗口不旋转但其余窗口旋转的单 View 应用程序模板创建应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42752361/

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