获取iPad的当前方向?

在给定的事件处理程序(而不是“shouldAutorotateToInterfaceOrientation”方法)中,如何检测当前的iPad方向? 我有一个文本字段,我必须animation了(当键盘出现)在横向视图,但不是在纵向视图,并想知道我在哪里,看看是否有必要的animation。

方向信息不是很一致,有几种方法。 如果在视图控制器中,可以使用interfaceOrientation属性。 从其他地方你可以打电话:

 [[UIDevice currentDevice] orientation] 

或者,您可以请求接收方向更改通知:

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

有些人也喜欢检查状态栏方向:

 [UIApplication sharedApplication].statusBarOrientation 

我认为

 [[UIDevice currentDevice] orientation]; 

是不是真的可靠。 有时候它有效,有时候不会…在我的应用程序中,我使用

 [[UIApplication sharedApplication]statusBarOrientation]; 

而且效果很好!

之一:

  • 检查活动视图控制器的interfaceOrientation属性。
  • [UIApplication sharedApplication].statusBarOrientation
  • [UIDevice currentDevice].orientation 。 (您可能需要调用-beginGeneratingDeviceOrientationNotifications 。)

我find了一个解决FaceUp方向问题的技巧!

延迟方向检查,直到应用程序开始运行后,然后设置variables,视图尺寸等!

 //CODE - (void)viewDidLoad { [super viewDidLoad]; //DELAY [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(delayedCheck) userInfo:nil repeats:NO]; } -(void)delayedCheck{ //DETERMINE ORIENTATION if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait ){ FACING = @"PU"; } if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown ){ FACING = @"PD"; } if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ){ FACING = @"LL"; } if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight ){ FACING = @"LR"; } //DETERMINE ORIENTATION //START [self setStuff]; //START } -(void)setStuff{ if( FACING == @"PU" ){ //logic for Portrait } else if( FACING == @"PD" ){ //logic for PortraitUpsideDown } else{ if( FACING == @"LL"){ //logic for LandscapeLeft } else if( FACING == @"LR" ){ //logic for LandscapeRight } } //CODE 

你可以在'setStuff'函数中添加子视图,位置元素等任何最初取决于方向的东西!

:d

克里斯·阿林森

你可以通过两种方式实现这一点:

1-通过使用以下方法:

**将以下行放在-(void)viewDidLoad方法中:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

然后把这个方法放在你的类中

 -(void)deviceRotated:(NSNotification*)notification { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { //Do your textField animation here } } 

上述方法将检查设备旋转的方向

第二种方法是在-(void)viewDidLoad插入以下通知

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRotation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; 

然后把下面的方法放在你的类中

 -(void)checkRotation:(NSNotification*)notification { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { //Do your textField animation here } } 

上面的方法将检查ipad或iPhone的状态栏的方向,并根据它来做你的animation在所需的方向。

为了确定风景与肖像,有一个内置的function:

 UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; BOOL inLandscape = UIDeviceOrientationIsLandscape(orientation); 

[UIApplication sharedApplication].statusBarOrientation在横向时返回纵向,在iPad中启动时纵向为横向

我不知道为什么,但每次我的应用程序启动时,前4个是正确的,但随后我得到相反的方向。 我使用一个静态variables来计算这个,然后有一个BOOL来翻转我如何手动发送到子视图。

所以,虽然我没有添加新的独立答案,但我想说的是使用上面的内容并记住这一点。 注意:我正在接收状态栏方向,因为这是在应用程序启动时被调用的唯一东西,并且“足够”可以帮助我移动内容。

使用这个的主要问题是被延迟加载的视图。 一定要打电话给你包含的视图属性和子视图“之前”你设置他们的位置响应他们的方向。 当我们设置不存在的variables时,感谢苹果公司没有崩溃,迫使我们记住他们打破了面向对象,并迫使我们这样做… gah,这样一个优雅的系统如此破碎! 说真的,我喜欢Native,但是不好,鼓励糟糕的OOdevise。 不是我们的错,只是提醒您的resizefunction可能正在工作,但苹果的方式要求您加载视图使用,而不是创build和初始化

在您的视图控制器中,获取self.interfaceOrientation的只读值(接口的当前方向)。

我已经尝试了许多上述方法,但是对我来说没有任何东西可以100%的工作。

我的解决scheme是在根视图控制器中创build一个名为UIInterfaceOrientationtypes的iVar方向。

 - (void)viewDidLoad { [super viewDidLoad]; orientation = self.interfaceOrientation; // this is accurate in iOS 6 at this point but not iOS 5; iOS 5 always returns portrait on app launch through viewDidLoad and viewWillAppear no matter which technique you use. } - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ return YES; } -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ orientation = toInterfaceOrientation; } 

那么,在任何你需要检查方向的地方你都可以这样做:

  if(UIInterfaceOrientationIsPortrait(orientation)){ // portrait }else{ // landscape } 

可能还有更好的办法,但这似乎98%的时间工作(iOS5尽pipe),并不难。 请注意,iOS5总是在纵向视图中启动iPad,然后向设备发送willRotateTo-和didRotateFromInterfaceOrientation:消息,因此该值将仍然不准确。

[UIDevice currentDevice].orientation很好。

但!!! 诀窍是将其添加到 – (void)viewWillAppear:(BOOL)animated

EXP:

 (void)viewWillAppear:(BOOL)animated{ ... BOOL isLandscape = UIInterfaceOrientationIsLandscape(self.interfaceOrientation); ... } 

如果在 – (void)viewDidLoad调用它,它不可靠,特别是如果你使用多个线程(主UI线程,后台线程访问大量的外部数据…)。


评论:1)即使你的应用程序设置默认的方向肖像,用户可以locking在横向。 因此,设置默认值并不是真正的解决方法。 2)还有其他一些任务,比如隐藏导航栏,放置在viewWillAppear上,使其工作,同时防止闪烁。 同样适用于其他视图,如UITableView willDisplayCell – >使用它来设置cell.selected和cell.accessoryType。