【问题标题】:iPad:Lock UIView orientation into Landscape mode onlyiPad:仅将 UIView 方向锁定为横向模式
【发布时间】:2011-05-31 14:23:12
【问题描述】:

此要求适用于 iPad。 我有一个 UIView,它在启动视频录制时用作相机覆盖视图。它在 UIImagePickerController 中设置如下。

[self presentModalViewController:pickerController animated:NO];
pickerController.cameraOverlayView =myOwnOverlay;

这是我的要求,即在调用摄像头进行视频录制时,我必须在 UIImagePickerController 中提供自己的叠加层。 我只想将自己的相机覆盖 UIView 锁定为横向模式,以便用户只能在横向模式下录制视频,而不是纵向模式,这也是我的项目要求。 我知道用于 UIViewController 的 shouldAutorotateToInterfaceOrientation。我使用了 "[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];" 它,当我启动此相机自己的叠加 UIView 时,它会锁定为横向模式,但是当我旋转设备时,UIView 也会旋转为纵向。我根本不想要人像模式。我试图像下面这样处理这个问题,但它不适用于 UIView。然后我看到这在 UIviewController 中是可能的,但在 UIView 中是不可能的。但是我必须有 UIView 才能进行此相机启动操作。

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

return (interfaceOrientation==UIInterfaceOrientationLandscapeRight || interfaceOrientation==UIInterfaceOrientationLandscapeLeft);
}

请建议我如何为我的 UIView 提供方向锁定解决方案?

谢谢!

【问题讨论】:

    标签: ipad


    【解决方案1】:

    怀疑 APPLE 是否允许,但如果您希望 UIImagePickerController 以横向方式启动(并保持),请使用以下代码。

    //Initialize picker
    
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
       picker.delegate = self;
    
    
    //set Device to Landscape. This will give you a warning. I ignored it.
    //warning: 'UIDevice' may not respond to '-setOrientation:'
    
    
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
    
    //Set Notifications so that when user rotates phone, the orientation is reset to landscape.
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    
    //Refer to the method didRotate:   
    [[NSNotificationCenter defaultCenter] addObserver:self
                  selector:@selector(didRotate:)
                   name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    
    //Set the picker source as the camera   
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    //Bring in the picker view   
    [self presentModalViewController:picker animated:YES];
    

    方法didRotate:

    - (void) didRotate:(NSNotification *)notification
    
    {
          //Maintain the camera in Landscape orientation
     [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
    
    }
    

    此代码的信用属于UIImagePickerController in Landscape

    【讨论】:

    • 但在 didRotate 方法中有时应用程序崩溃
    【解决方案2】:

    试试这个:

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    
    {
        BOOL isLandscapeRight = (UIInterfaceOrientationLandscapeRight == interfaceOrientation);
        return isLandscapeRight;
    }
    

    还必须将您的应用程序 info.plist 的界面方向设置为横向(右主页按钮)

    【讨论】:

      【解决方案3】:

      有一个简单的解决方案 在 info.plist 文件中编辑条目支持的界面方向 ipad。

      【讨论】:

        猜你喜欢
        • 2012-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多