如何使用“单视图应用程序"模板创建应用程序,而主窗口不会旋转,而其他窗口会旋转? [英] How to create an App using the Single View App template where the main window does not rotate but the rest does?

查看:124
本文介绍了如何使用“单视图应用程序"模板创建应用程序,而主窗口不会旋转,而其他窗口会旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Single View App模板创建一个应用,该应用的主窗口不旋转,但其rootViewController和其他所有窗口自动旋转?

How to create an App using the Single View App template where the main window does not rotate but its rootViewController and everything else autorotates?

Apple在 CIFunHouse 上进行了此操作在那件事上,代码解释得很糟糕,无法知道他们是如何做到的.如果您运行该应用程序,将会看到相机的预览窗口不会自动旋转,因为预览已添加到窗口中,而其他所有操作都可以.

Apple does that on CIFunHouse but because the code is poorly explained in that matter, it is impossible to know how they did it. If you run the app you will see that the camera's preview window does not autorotate because the preview was added to the window but everything else does.

Apple在其本机iPad相机应用程序中使用了此技术.

Apple uses this technique on their native iPad camera app.

推荐答案

所以答案并不干净,但我可以给您解决.在某个时候,主应用程序窗口一定不能像现在一样自动旋转,但在某个时候,它会根据rootviewcontroller开始旋转.至少此源代码表明了这一点.我从iOS 6结束时就开始开发,我认为此资料是在那个时候写的.我可以找到允许示例中所有内容旋转但让预览不旋转的最佳解决方案是添加第二个窗口.将主窗口背景设置为清除.然后将PreviewLayer添加到主窗口后面的第二个窗口中.在代码中看起来像这样.

So the answer is not clean but I can give you a fix. At some point the main application window must not have autorotated like it does now but at some point it started rotating according to the rootviewcontroller. At least this source code suggests that. I started developing at the end of iOS 6 and I think this source was written about that time. The best fix I could find for allowing everything to rotate in the example for me but having the preview to not rotate was to add a second window. Set the main window background to clear. Then add the previewLayer to the second window behind the main window. In code it would look like this.

AppDelegate看起来像这样.

The AppDelegate looked like this.

#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.

Then in the viewDidLoad of the FHViewController instead of adding to the main window I did this and it added where they get the main window I add the previewView to that.

   // 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上对我有效.

Because the previewWindow has to have a rootviewcontroller and it determines the rotation you can see my testviewcontroller has autorotate of NO. Hope this helps. It is working for me on iOS 10.

上面示例中的视图不会旋转,但是旋转时的窗口动画在视觉上是不好的.可以通过覆盖将其删除 willTransitionToSize

The view in the example above does not rotate but the window animation on rotation is bad visually. It can be removed by overriding willTransitionToSize

    [UIView setAnimationsEnabled:NO];

完成后

[UIView setAnimationsEnabled:YES];

GitHub

对于需要iPad的iPad,必须选中全屏".

For an iPad requires Full Screen must be check.

这篇关于如何使用“单视图应用程序"模板创建应用程序,而主窗口不会旋转,而其他窗口会旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆