iOS 8中不推荐使用didRotateFromInterfaceOrientation

iOS 8中不推荐使用didRotateFromInterfaceOrientation,ios,objective-c,ios8,Ios,Objective C,Ios8,从iOS 8.0开始,函数didRotateFromInterfaceOrientation就被弃用了,我现在不知道如何正确地更改的方向 根据,我有以下工作代码,可以基于statusBarOrientation获取AVCaptureVideoOrientation: - (AVCaptureVideoOrientation)getOrientation { // Translate the orientation switch ([[UIApplication sharedApp

从iOS 8.0开始,函数
didRotateFromInterfaceOrientation
就被弃用了,我现在不知道如何正确地更改的方向

根据,我有以下工作代码,可以基于
statusBarOrientation
获取
AVCaptureVideoOrientation

- (AVCaptureVideoOrientation)getOrientation {
    // Translate the orientation
    switch ([[UIApplication sharedApplication] statusBarOrientation]) {
        case UIInterfaceOrientationPortrait:
            return AVCaptureVideoOrientationPortrait;
        case UIInterfaceOrientationPortraitUpsideDown:
            return AVCaptureVideoOrientationPortraitUpsideDown;
        case UIInterfaceOrientationLandscapeLeft:
            return AVCaptureVideoOrientationLandscapeLeft;
        case UIInterfaceOrientationLandscapeRight:
            return AVCaptureVideoOrientationLandscapeRight;
        default:
            return AVCaptureVideoOrientationPortrait;
    }
}
当方向改变时,我需要调用以下函数:

[self.videoPreviewLayer.connection setVideoOrientation:[self getOrientation]];
但我不知道该把它放在哪里

尝试1 由于
didRotateFromInterfaceOrientation
已被弃用,苹果建议使用
ViewWillTransitionOnSize:WithTransitionOrdinator:
,这似乎是很自然的尝试:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        [self.videoPreviewLayer.connection setVideoOrientation:[self getOrientation]];
    }];
}
在iPad的横向和纵向模式下,显示的视图大小相同:

因此,我不能简单地使用
viewwillTransitionOnToSize

尝试2 我注意到,
UIDeviceOrientationIDChangeNotification
没有被弃用。伟大的我尝试注册一个通知:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Some other code to set up the preview layer 
    // ...

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(deviceOrientationDidChange:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)deviceOrientationDidChange:(NSNotification *)notification {
    [self.videoPreviewLayer.connection setVideoOrientation:[self getOrientation]];
}
这是几乎工作,它是如此封闭!问题是,当我收到设备方向更改通知时,旋转视图的动画可能会启动,也可能不会启动。或者换句话说,如果设备已上下旋转,但动画仍处于挂起状态,则返回值
[[UIApplication sharedApplication]statusBarOrientation]
可能不正确


我不知道还能尝试什么:(有什么想法吗?

以下内容应该能够在swift 2.1中正确处理视频预览的方向:

override func viewWillAppear(animated: Bool) {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self,
        selector: Selector("deviceOrientationDidChange:"),
        name: UIDeviceOrientationDidChangeNotification,
        object: nil)
}

override func viewDidDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self)
    super.viewDidDisappear(animated)
}

func deviceOrientationDidChange(sender: AnyObject) {
    let deviceOrientation = UIDevice.currentDevice().orientation;
    switch (deviceOrientation)
    {
    case .Portrait:
       previewLayer.connection.videoOrientation = .Portrait
    case .LandscapeLeft:
       previewLayer.connection.videoOrientation = .LandscapeRight
    case .LandscapeRight:
        previewLayer.connection.videoOrientation = .LandscapeLeft
    case .PortraitUpsideDown:
       previewLayer.connection.videoOrientation = .PortraitUpsideDown
    default:
        break
    }
}

为什么不在“deviceOrientationDidChange”中使用UIDeviceOrientation方法,而不是状态栏方向?只需忽略.FaceUp和.FaceDown方向。@almas,谢谢您的评论。但问题是设备方向与界面方向不完全相同。设备可能是朝上向下的,但视图不支持.FaceDown。如果我能保证我当前的view支持所有四个方向,那么,是的,我认为你的建议在这里是可以的!好吧,你只需要忽略你的应用程序不支持的所有方向,例如,你可以忽略FaceUp、FaceDown和GraphitoUpsideDown。试试看,我相信它会起作用。我希望苹果能在幕后实现你的方式。做得好。你仍然可以l使用不推荐使用的
didRotate
,但您的实现实际上考虑了一些边缘情况,而
didRotate
没有。
override func viewWillAppear(animated: Bool) {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self,
        selector: Selector("deviceOrientationDidChange:"),
        name: UIDeviceOrientationDidChangeNotification,
        object: nil)
}

override func viewDidDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self)
    super.viewDidDisappear(animated)
}

func deviceOrientationDidChange(sender: AnyObject) {
    let deviceOrientation = UIDevice.currentDevice().orientation;
    switch (deviceOrientation)
    {
    case .Portrait:
       previewLayer.connection.videoOrientation = .Portrait
    case .LandscapeLeft:
       previewLayer.connection.videoOrientation = .LandscapeRight
    case .LandscapeRight:
        previewLayer.connection.videoOrientation = .LandscapeLeft
    case .PortraitUpsideDown:
       previewLayer.connection.videoOrientation = .PortraitUpsideDown
    default:
        break
    }
}