如何在以下应用程序状态中找到设备方向:
答案 0 :(得分:0)
如果您按照设置方向启用设备方向通知,则可以获取有关设备方向更改的通知,但我不确定您是否可以在启动时找到当前方向那样。
如果您在需要设备方向时碰巧在视图控制器代码内部,那么在您需要方向时,最好只询问视图控制器,例如:
self.interfaceOrientation
当self
是UIViewController的一个实例时。通常重要的是知道你是在纵向模式还是横向模式,在这种情况下你会有类似的东西:
const UIInterfaceOrientation currentInterfaceOrientation = self.interfaceOrientation;
if (UIInterfaceOrientationIsLandscape(currentInterfaceOrientation)) {
// Set up for landscape orientation.
} else {
// Set up for portrait orientation (including upside-down orientation).
}
修改:您需要对application:didFinishLaunching:withOptions:
方法之外的设备方向进行初步检查。您可以使用dispatch_after
来延迟执行设备方向的初始检查。我不确定我喜欢你在这里使用的模式,但我认为这对你有用,在application:didFinishLaunchingWithOptions:
方法结束时:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
__block UIDeviceOrientation initialDeviceOrientation;
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
initialDeviceOrientation = [[UIDevice currentDevice] orientation];
NSLog(@"initialDeviceOrientation = %u", initialDeviceOrientation);
});
// Etc.
答案 1 :(得分:0)
您可以在第一次启动应用程序时检测方向,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];
}
检测方向:
-(void)deviceOrientationDidChange:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
//Ignoring specific orientations
if (orientation == UIDeviceOrientationFaceUp || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationUnknown || currentOrientation == orientation)
{
//To check orientation ;
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"deveiceorientation"])
{
// your orientation
}
else
{
[[NSUserDefaults standardUserDefaults] setObject:deviceOrientaion forKey:@"deveiceorientation"];
[[NSUserDefaults standardUserDefaults] synchronize];
// save your orientation
}
}
在应用程序输入前景时,您可以使用
检查相同内容-(void)viewDidAppear:(BOOL)animated{
}