简体   繁体   English

iPhone SDK - shouldAutorotateToInterfaceOrientation 问题

[英]iPhone SDK - shouldAutorotateToInterfaceOrientation Question

I am trying to run some code in my iPhone app when the device is rotated left or right.当设备向左或向右旋转时,我试图在我的 iPhone 应用程序中运行一些代码。 Clearly, I can do this using shouldAutorotateToInterfaceOrientation , However, I DO NOT want to rotate the controls/subviews on the view.显然,我可以使用shouldAutorotateToInterfaceOrientation来做到这一点,但是,我不想旋转视图上的控件/子视图。 I can stop the subviews from rotating by returning NO to the above method:我可以通过向上述方法返回NO来阻止子视图旋转:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    
    NSLog(@"Should rotate\n");
    return YES;
}

When I do this and rotate the device left or right, the event fires as expected.当我这样做并向左或向右旋转设备时,事件会按预期触发。 My problem is, when I rotate back from landscape to portrait, the event isn't fired.我的问题是,当我从横向旋转回纵向时,不会触发该事件。 This has to be because the rotation was stopped by returning NO before it was rotated to lanscape;这必须是因为在旋转到横向之前通过返回NO停止了旋转; so the orientation is still considered portrait.所以方向仍然被认为是纵向的。 However, if I return YES , the subviews rotate.但是,如果我返回YES ,子视图会旋转。

Has anyone dealt with this issue?有没有人处理过这个问题? Does anyone know a way to either (1) return YES with shouldAutorotateToInterfaceOrientation while keeping the controls static, or (2) capture the device rotation independent of shouldAutorotateToInterfaceOrientation ?有谁知道一种方法可以(1)在保持控件 static 的同时使用shouldAutorotateToInterfaceOrientation返回YES ,或者(2)独立于shouldAutorotateToInterfaceOrientation捕获设备旋转?

Many thanks, Brett非常感谢,布雷特

If you need to just follow the device rotations, you can do this by listening for UIDeviceOrientationDidChangeNotification notification and then getting the current orientation using the orientation property of the UIDevice singleton.如果您只需要跟随设备旋转,您可以通过侦听UIDeviceOrientationDidChangeNotification通知然后使用UIDevice singleton 的orientation属性获取当前方向来做到这一点。 You will have to start and stop these notifications by calling beginGeneratingDeviceOrientationNotifications and endGeneratingDeviceOrientationNotifications on the UIDevice object.您必须通过调用UIDevice object 上的beginGeneratingDeviceOrientationNotificationsendGeneratingDeviceOrientationNotifications来启动和停止这些通知。

This way you can return YES only for the orientation you want the controls to be in and yet follow device orientation changes.这样,您可以仅针对您希望控件所处的方向返回 YES 并遵循设备方向的更改。

While the purpose isn't clear, if this is for a game you should look at CoreMotion framework and follow the gyro-meter data.虽然目的尚不清楚,但如果这是为了游戏,您应该查看CoreMotion框架并遵循陀螺仪数据。

You could register an event and fire it from inside shouldAutorotateToInterfaceOrientation with the needed orientation, while always returning NO.您可以注册一个事件并从 shouldAutorotateToInterfaceOrientation 内部以所需的方向触发它,同时始终返回 NO。

Disclaimer: Might sound a bit hacky, but it will give you manual control over the interface while changing orientation.免责声明:可能听起来有点老套,但它可以让您在更改方向时手动控制界面。

shouldAutorotateToInterfaceOrientation isn't an event. shouldAutorotateToInterfaceOrientation不是事件。 It's simply meant to take a UIInterfaceOrientation and return YES or NO depending on whether or not autorotation is supported.它只是意味着采用UIInterfaceOrientation并根据是否支持自动旋转返回 YES 或 NO。

The Apple docs show that UIInterfaceOrientation is defined as: Apple 文档显示UIInterfaceOrientation定义为:

   typedef enum {
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft 
    }UIInterfaceOrientation;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM