了解iPad的当前方向?

了解iPad的当前方向?,ipad,orientation,Ipad,Orientation,在给定的事件处理程序中(不是“shouldAutorotateToInterfaceOrientation”方法),如何检测当前的iPad方向?我有一个文本字段,我必须在横向视图中设置动画(当键盘出现时),但不能在纵向视图中设置动画,我想知道我所处的方向,以查看动画是否必要。以下之一: 检查活动视图控制器的interface-orientation属性 [UIApplication sharedApplication].StatusBaroOrientation [UIDevice curre

在给定的事件处理程序中(不是“shouldAutorotateToInterfaceOrientation”方法),如何检测当前的iPad方向?我有一个文本字段,我必须在横向视图中设置动画(当键盘出现时),但不能在纵向视图中设置动画,我想知道我所处的方向,以查看动画是否必要。

以下之一:

  • 检查活动视图控制器的
    interface-orientation
    属性
  • [UIApplication sharedApplication].StatusBaroOrientation
  • [UIDevice currentDevice]。方向
    。(您可能需要调用
    -begingeratingdeviceorientationnotifications

    • 方向信息不太一致,有几种方法。如果在视图控制器中,则可以使用
      interface-orientation
      属性。您可以从其他地方拨打:

      [[UIDevice currentDevice] orientation]
      
      或者,您可以请求接收方向更改通知:

      [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
      
      有些人还喜欢检查状态栏的方向:

      [UIApplication sharedApplication].statusBarOrientation
      

      我找到了一个解决FaceUp定向问题的窍门

      延迟方向检查直到应用程序开始运行,然后设置变量、视图大小等

      //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

      -克里斯·阿林森

      我想

      [[UIDevice currentDevice] orientation];
      
      这不是很可靠。有时有效,有时无效。。。在我的应用程序中,我使用

      [[UIApplication sharedApplication]statusBarOrientation]; 
      

      而且效果很好

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

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


      使用此选项的主要问题是视图被延迟加载。确保在设置包含视图和子视图的位置以响应其方向之前,调用它们的view属性。感谢苹果在我们设置不存在的变量时没有崩溃,迫使我们记住它们破坏了OO,也迫使我们这么做。。。唉,这么优雅的系统还这么破烂!说真的,我喜欢Native,但它并不好,会鼓励糟糕的OO设计。这不是我们的错,只是提醒您的调整大小功能可能正在工作,但苹果的方式要求您通过使用来加载视图,而不是通过创建和初始化视图来加载视图

      但是!!! ... 诀窍是将其添加到-
      (void)视图将显示:(BOOL)动画

      经验:

      如果您在-
      (void)viewDidLoad
      处调用它,则其工作不可靠,尤其是如果您使用多个线程(主UI线程、后台线程来访问大量外部数据,…)


      评论: 1) 即使您的应用程序设置了默认方向肖像,用户也可以将其锁定在横向。因此,设置默认值并不是解决问题的真正方法。
      2) 还有其他任务,如隐藏要放置在视图中的导航栏,以使其工作,同时防止闪烁。这同样适用于其他视图,如UITableView
      willDisplayCell
      ->使用它设置cell.selected和cell.accessoryType。

      为了确定横向与纵向,有一个内置功能:

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

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

      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)checkRotation:(NSNotification*)notification
      {
          UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
          if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
          {
               //Do your textField animation here
          }
      }
      
      当设备旋转时,上述方法将检查方向

      2-第二种方法是在
      -(void)viewDidLoad

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkRotation:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
      
      然后将以下方法放入类中

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

      上述方法将检查ipad或iPhone状态栏的方向,并根据它以所需的方向制作动画。

      在视图控制器中,获取self.interface定向的只读值(界面的当前方向)

      [UIApplication sharedApplication].StatusBaroOrientation
      在iPad中,当它是横向时返回纵向,当它是纵向时返回横向,我已经尝试了上述许多方法,但似乎没有任何方法100%适用于我

      我的解决方案是在根视图控制器中创建一个名为UIInterfaceOrientation类型定向的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),而且并不太难。请注意,iOS5总是以纵向视图启动iPad,然后向设备发送willRotateTo-和didRotateFromInterfaceOrientation:消息,因此该值在短期内仍然不准确。

      这对我帮助很大。我使用的是[[UIDevice currentDevice]方向],但当您正面启动设备时,无法判断接口的方向。使用[UIApplication sharedApplication].StatusBaroOrientation为我解决了这个问题。非常感谢。请注意,[[UIDevice currentDevice]定向]将返回0,除非已启用定向通知。[[UIDevice currentDevice]定向]有错误。这浪费了我太多的时间。谢谢..注意:[[UIDevice currentDevice]定向]获取设备旋转,而不是接口旋转!如果用户锁定了设备的方向,则设备方向和界面方向可能会非常不同。statusBarOrientation返回一个界面方向,当用户锁定其设备的方向时,该方向是正确的。不要为此使用
      [[UIDevice currentDevice]方向]
      :属性的值是常量