Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS 7/8中的app start上获得初始设备定向?_Ios_Objective C_Rotation_Orientation - Fatal编程技术网

如何在iOS 7/8中的app start上获得初始设备定向?

如何在iOS 7/8中的app start上获得初始设备定向?,ios,objective-c,rotation,orientation,Ios,Objective C,Rotation,Orientation,我正在实现一个类似于苹果相机应用程序的照片/视频拾取界面。我需要手动旋转界面,因为我不希望预览视图旋转,但我确实希望相应地旋转控件。因此,我的视图控制器(应用程序的根视图控制器)中有以下代码: 为了检测方向变化,我在viewDidLoad中注册了相应通知的VC: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dev

我正在实现一个类似于苹果相机应用程序的照片/视频拾取界面。我需要手动旋转界面,因为我不希望预览视图旋转,但我确实希望相应地旋转控件。因此,我的视图控制器(应用程序的根视图控制器)中有以下代码:

为了检测方向变化,我在
viewDidLoad
中注册了相应通知的VC:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(deviceOrientationDidChange)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:[UIDevice currentDevice]];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
这与预期的效果一样,除非我以除纵向以外的方向启动应用程序。我的回调被调用;但是,当前的设备方向始终是
UIDeviceOrientationRotation
,即使我在启动应用程序时侧身握住设备也是如此。状态栏方向也是如此

我如何在应用程序启动后立即确定设备方向?我知道这是可能的,因为苹果正在他们的相机应用程序中做这件事。问题是,是否有任何官方方式,或者他们是否使用一些神奇的内部API

更新 安娜的答案是正确的,但你也应该阅读评论。
TL;DR:当应用程序未针对iPhone 6(Plus)进行优化时,这种效果会在iPhone 6(Plus)上出现。

首先,您不想查看UI设备的方向(请参阅)

我认为在UI完成自身配置之前,您得到的是一个虚假的结果。换句话说,它报告纵向,因为它在启动过程中的那个时刻还没有准备好。为什么它要发送通知呢?不确定;但是,您肯定不想将它们用于您的任务

我不太明白您使用初始方向的目的,但我有另一种方法来解决您的问题:您可以通过反向旋转视图来保持视图静止。当视图控制器收到旋转消息(即viewDidRotate,iOS 8之前版本)时,可以将静态视图上的变换更改为沿与该旋转相反的方向旋转

使用autolayout也可能有一些聪明的方法,但我还没有弄明白它们…:-p

编辑:

一些代码

YMMV——这个管用,但我有一阵子没碰它了!你可能得玩一玩。。。与我在评论中所说的相反,看起来我正在为旋转设置动画。但是,它真的不动了!我认为反旋转动画会被整个显示系统的旋转所抵消。。。或者别的什么

simViewControllerContainer是我阻止旋转的视图

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set simView container's orientation if the device started in landscape
    if ( self.interfaceOrientation == UIInterfaceOrientationLandscapeRight ) {
        simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
        simViewControllerContainer.transform = CGAffineTransformMakeRotation(-M_PI/2);
    }
    else if ( self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
        simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
        simViewControllerContainer.transform = CGAffineTransformMakeRotation(M_PI/2);
    }
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    BOOL doubleRotation =
    (UIInterfaceOrientationIsPortrait(self.interfaceOrientation) && UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) ||
    (UIInterfaceOrientationIsLandscape(self.interfaceOrientation) && UIInterfaceOrientationIsLandscape(toInterfaceOrientation));

    if ( toInterfaceOrientation == UIInterfaceOrientationLandscapeRight ) {
        [UIView animateWithDuration:duration animations:^{
            if ( !doubleRotation ) {
                // For a double-rotation, we don't want to change the center -- it's the same:
                // a double-rotation is landscape to landscape or portrait to portrait -- same center.
                simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
            }
            simViewControllerContainer.transform = CGAffineTransformMakeRotation(-M_PI/2);
        }];
    }
    else if ( toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
        [UIView animateWithDuration:duration animations:^{
            if ( !doubleRotation ) {
                simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
            }
            simViewControllerContainer.transform = CGAffineTransformMakeRotation(M_PI/2);
        }];

    }
    else if ( toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ) {
        [UIView animateWithDuration:duration animations:^{
            if ( !doubleRotation ) {
                simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
            }
            simViewControllerContainer.transform = CGAffineTransformIdentity;
        }];

    }
    else if ( [UIDevice currentDevice].orientation == UIDeviceOrientationPortrait ) {
        [UIView animateWithDuration:duration animations:^{
            if ( !doubleRotation ) {
                simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
            }
            simViewControllerContainer.transform = CGAffineTransformIdentity;
        }];
    }
}

谢谢你的帮助,安娜。我的目标是模仿苹果相机应用程序的行为,它不会旋转窗口。通常,当应用程序界面旋转时,您可以看到屏幕矩形旋转。但是,Camera应用程序不会这样做,它只会旋转控件。这是有意义的,因为显示在背景中的相机馈送,无论如何都会随着相机旋转。如果我要旋转背景视图,我就必须反其道而行之。我主要需要方向来设置用于正确录制视频的
AVAssetWriter
的变换。如果不告诉它传入视频样本的方向,它将以默认方向保存视频。因此,在纵向拍摄的视频在观看录制的视频时会旋转。当您通过设置变换来反向旋转视图时,它实际上不会移动,也不会设置动画。iOS知道不移动它,因为转换与它打算执行的转换“匹配”。你能在视频上设置转换吗?也许它也会做类似的事情。这很乏味,但似乎是唯一的办法。关于如何设置视图的变换以精确反作用旋转,您有一些示例代码吗?那真的很有帮助。再次感谢你的帮助!要得到这个答案,请查看这个帖子。
- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set simView container's orientation if the device started in landscape
    if ( self.interfaceOrientation == UIInterfaceOrientationLandscapeRight ) {
        simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
        simViewControllerContainer.transform = CGAffineTransformMakeRotation(-M_PI/2);
    }
    else if ( self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
        simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
        simViewControllerContainer.transform = CGAffineTransformMakeRotation(M_PI/2);
    }
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    BOOL doubleRotation =
    (UIInterfaceOrientationIsPortrait(self.interfaceOrientation) && UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) ||
    (UIInterfaceOrientationIsLandscape(self.interfaceOrientation) && UIInterfaceOrientationIsLandscape(toInterfaceOrientation));

    if ( toInterfaceOrientation == UIInterfaceOrientationLandscapeRight ) {
        [UIView animateWithDuration:duration animations:^{
            if ( !doubleRotation ) {
                // For a double-rotation, we don't want to change the center -- it's the same:
                // a double-rotation is landscape to landscape or portrait to portrait -- same center.
                simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
            }
            simViewControllerContainer.transform = CGAffineTransformMakeRotation(-M_PI/2);
        }];
    }
    else if ( toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ) {
        [UIView animateWithDuration:duration animations:^{
            if ( !doubleRotation ) {
                simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
            }
            simViewControllerContainer.transform = CGAffineTransformMakeRotation(M_PI/2);
        }];

    }
    else if ( toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ) {
        [UIView animateWithDuration:duration animations:^{
            if ( !doubleRotation ) {
                simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
            }
            simViewControllerContainer.transform = CGAffineTransformIdentity;
        }];

    }
    else if ( [UIDevice currentDevice].orientation == UIDeviceOrientationPortrait ) {
        [UIView animateWithDuration:duration animations:^{
            if ( !doubleRotation ) {
                simViewControllerContainer.center = (CGPoint) {self.view.bounds.size.height/2, self.view.bounds.size.width/2};
            }
            simViewControllerContainer.transform = CGAffineTransformIdentity;
        }];
    }
}