&引用;“界面定向”;在iOS 8中已弃用,如何更改此方法目标C

&引用;“界面定向”;在iOS 8中已弃用,如何更改此方法目标C,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我从使用此方法的Cocoa控件下载了simpleCamera视图 - (AVCaptureVideoOrientation)orientationForConnection { AVCaptureVideoOrientation videoOrientation = AVCaptureVideoOrientationPortrait; switch (self.interfaceOrientation) { case UIInterfaceOrientationL

我从使用此方法的Cocoa控件下载了simpleCamera视图

- (AVCaptureVideoOrientation)orientationForConnection
{
    AVCaptureVideoOrientation videoOrientation = AVCaptureVideoOrientationPortrait;
    switch (self.interfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            videoOrientation = AVCaptureVideoOrientationLandscapeLeft;
            break;
        case UIInterfaceOrientationLandscapeRight:
            videoOrientation = AVCaptureVideoOrientationLandscapeRight;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            videoOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
            break;
        default:
            videoOrientation = AVCaptureVideoOrientationPortrait;
            break;
    }
    return videoOrientation;
}

问题是iOS 8中不推荐使用“interfaceOrientation”,但我不知道在切换条件下如何替换它

获取设备方向不正确。例如,设备可能处于横向模式,但仅支持纵向的视图控制器将保持纵向

相反,请使用以下命令:

[[UIApplication sharedApplication] statusBarOrientation]

另一个好处是它仍然使用UIInterfaceOrientation枚举,因此您的代码几乎不需要更改。

使用UIDevice的
-orientation
属性是不正确的(即使它在大多数情况下都可以工作),并且可能导致一些错误,例如,<>代码> UIDEVICIORACTION/COD>考虑设备的朝向,如果它是向上或向下,在这些值中没有<代码> Ui接口面向/代码>枚举。 此外,如果您将应用程序锁定在某个特定方向,UIDevice将为您提供设备方向,而不考虑该方向。
另一方面,iOS8不赞成
UIViewController
类上的
interfaceOrientation
属性
有两个选项可用于检测接口方向:

  • 使用状态栏方向
  • 在iPhone上使用大小类,如果它们没有被覆盖,它们可以让您了解当前的界面方向
仍然缺少的是一种理解界面方向变化方向的方法,这在动画制作过程中非常重要。
在WWDC 2014“iOS8中的视图控制器改进”课程中,演讲者也提供了该问题的解决方案,使用替换
-will/didrotateInterfaceOrientation
的方法

此处部分实施了建议的解决方案:

-(void) viewWillTransitionToSize:(CGSize)s withTransitionCoordinator:(UIVCTC)t {
    orientation = [self orientationFromTransform: [t targetTransform]]; 
    oldOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    [self myWillRotateToInterfaceOrientation:orientation duration: duration]; 
    [t animateAlongsideTransition:^(id <UIVCTCContext>) {
         [self myWillAnimateRotationToInterfaceOrientation:orientation
                                                  duration:duration];
      }
      completion: ^(id <UIVCTCContext>) {
         [self myDidAnimateFromInterfaceOrientation:oldOrientation];
      }];
}
-(void)视图将传递大小:(CGSize)s和传递协调员:(UIVCTC)t{
方向=[self-orientationFromTransform:[t targetTransform]];
oldOrientation=[[UIApplication sharedApplication]statusBarOrientation];
[自我意志旋转界面定向:定向持续时间:持续时间];
[t AnimateLongsideTransition:^(id){
[自我意志动画制作界面定向:定向
持续时间:持续时间];
}
完成日期:^(id){
[自我myDidAnimateFromInterfaceOrientation:oldOrientation];
}];
}

自iOS8以来,苹果建议使用TraitCollections(大小类)而不是interfaceOrientation

此外,由于iOS 9和新的iPad具有“多任务”功能,因此在某些情况下,设备方向与窗口比例不匹配!(破坏应用程序UI)

因此,在使用常规大小的类时,您也应该非常小心,因为它不必占用整个iPad屏幕

有时候traitcollection并不能满足您所有的设计需求。对于这些情况,苹果建议比较视图的边界:

if view.bounds.size.width > view.bounds.size.height {
    // ...
}
我很惊讶,但你可以在21点15分查看WWDC 2015视频


也许你真的在寻找设备的方向,而不是屏幕或窗口的比例。如果您关心设备摄像头,则不应使用TraitCollection,也不应使用view bounds

Swift 4+版本

UIApplication.shared.statusBarOrientation

使用
UIApplication.shared.statusBarOrientation
时,Xcode 11将显示警告
“statusBarOrientation”在iOS 13.0中被弃用:改用窗口场景的interfaceOrientation属性。

这个答案在Swift 5中,支持iOS 13和更低版本。它假定:

  • 您将只创建一个场景,而不会创建多个场景
  • 您希望将代码包含到特定视图中
  • 您的视图中有一个成员变量
    previewLayer
    ,类型为
    AVCaptureVideoPreviewLayer
首先,在
SceneDelegate.swift
中添加以下行:

    static var lastCreatedScene: UIWindowScene?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let newScene = (scene as? UIWindowScene) else { return }
        Self.lastCreatedScene = newScene
    }
然后在视图中,添加以下函数并在
layoutSubviews()
中调用它:


确实提到了它。谢谢有没有推荐的方法可以知道设备是左还是右?这与iphonex的性能有很大关系:(好吧,你可以使用
[[UIDevice currentDevice]orientation]
如果您想检查真实的设备方向,但不应将其用于iPhone X适配。请使用新的iOS 11安全区域或旧的布局边距。
statusBarOrientation
在iOS 9中不推荐使用-orientationFromTransform:method如何从转换中派生方向?@Chandra check在iOS 13中,此方法给出“'statusBarOrientation'已弃用:在iOS 13.0中首次弃用-改用窗口场景的interfaceOrientation属性。”消息
    func refreshOrientation() {
        let videoOrientation: AVCaptureVideoOrientation
        let statusBarOrientation: UIInterfaceOrientation
        if #available(iOS 13, *) {
            statusBarOrientation = SceneDelegate.lastCreatedScene?.interfaceOrientation ?? .portrait
        } else {
            statusBarOrientation = UIApplication.shared.statusBarOrientation
        }
        switch statusBarOrientation {
        case .unknown:
            videoOrientation = .portrait
        case .portrait:
            videoOrientation = .portrait
        case .portraitUpsideDown:
            videoOrientation = .portraitUpsideDown
        case .landscapeLeft:
            videoOrientation = .landscapeLeft
        case .landscapeRight:
            videoOrientation = .landscapeRight
        @unknown default:
            videoOrientation = .portrait
        }
        self.previewLayer.connection?.videoOrientation = videoOrientation
    }