8

我正在使用最新的 SDK 开发一个 iOS 应用程序。

我已将横向正确方向设置为可用的唯一方向,并且我对 viewController 视图有疑问。

这是我的代码:

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != AVCaptureVideoOrientationLandscapeRight);
}

- (void)deviceOrientationDidChange:(NSNotification*)notification
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == AVCaptureVideoOrientationLandscapeLeft)
        NSLog(@"Orientation: Landscape Left");
    else if (orientation == AVCaptureVideoOrientationLandscapeRight)
        NSLog(@"Orientation: Landscape Right");
    NSLog(@"FRAME: %@", NSStringFromCGRect(self.view.frame));
}

这是我得到的日志:

Orientation: Landscape Right
FRAME: {{0, 0}, {320, 568}}
Orientation: Landscape Right
FRAME: {{0, 0}, {320, 568}}

我的问题是关于{{0, 0}, {320, 568}}

为什么我会得到这些值?

我认为正确的值是{{0, 0}, {568, 320}}.

4

4 回答 4

3

我相信框架矩形不会对方向变化做出反应。

编辑:我最初的解决方案对于您的需求来说过于复杂。如果您正确找到当前方向,那么如果您处于横向模式,则只需将矩形的宽度解释为高度。

于 2013-03-19T16:37:38.020 回答
1

我添加了这个解决方案来展示框架和绑定会发生什么。

我添加了这三种方法:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
        CGRect bounds = [view bounds];
        NSLog(@"FRAME: %@", NSStringFromCGRect(view.frame));
        NSLog(@"BOUNDS: %@", NSStringFromCGRect(bounds));
}

- (void)deviceOrientationDidChange:(NSNotification*)notification
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == AVCaptureVideoOrientationLandscapeLeft)
        NSLog(@"1. Orientation: Landscape Left");
    else if (orientation == AVCaptureVideoOrientationLandscapeRight)
        NSLog(@"1. Orientation: Landscape Right");
    NSLog(@"1. FRAME: %@", NSStringFromCGRect(self.view.frame));
    NSLog(@"1. BOUNDS: %@", NSStringFromCGRect(self.view.bounds));
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == AVCaptureVideoOrientationLandscapeLeft)
        NSLog(@"2. Orientation: Landscape Left");
    else if (orientation == AVCaptureVideoOrientationLandscapeRight)
        NSLog(@"2. Orientation: Landscape Right");
    NSLog(@"2. FRAME: %@", NSStringFromCGRect(self.view.frame));
    NSLog(@"2. BOUNDS: %@", NSStringFromCGRect(self.view.bounds));
}

- (void)viewDidLayoutSubviews
{
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == AVCaptureVideoOrientationLandscapeLeft)
        NSLog(@"3. Orientation: Landscape Left");
    else if (orientation == AVCaptureVideoOrientationLandscapeRight)
        NSLog(@"3. Orientation: Landscape Right");
    NSLog(@"3. FRAME: %@", NSStringFromCGRect(self.view.frame));
    NSLog(@"3. BOUNDS: %@", NSStringFromCGRect(self.view.bounds));
}

这就是我得到的:

FRAME: {{0, 0}, {320, 568}}
BOUNDS: {{0, 0}, {320, 568}}
3. Orientation: Landscape Right
3. FRAME: {{0, 0}, {320, 568}}
3. BOUNDS: {{0, 0}, {568, 320}}
1. Orientation: Landscape Right
1. FRAME: {{0, 0}, {320, 568}}
1. BOUNDS: {{0, 0}, {568, 320}}
1. Orientation: Landscape Right
1. FRAME: {{0, 0}, {320, 568}}
1. BOUNDS: {{0, 0}, {568, 320}}

绑定更改viewDidLayoutSubviews:

于 2013-03-19T17:25:22.243 回答
0

使用 self.view.bounds 而不是 self.view.frame

于 2014-12-07T03:20:33.110 回答
0

请更改您的 viewController

 SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];  

替换为

 SKScene * scene = [MyScene sceneWithSize:CGSizeMake(skView.frame.size.height, skView.frame.size.width)];
于 2015-07-31T10:50:39.880 回答