Get current orientation of iPad?

后端 未结 11 1677
野趣味
野趣味 2020-12-02 07:28

In a given event handler (not the \"shouldAutorotateToInterfaceOrientation\" method) how do I detect the current iPad orientation? I have a text field I have to animate up

相关标签:
11条回答
  • 2020-12-02 08:12

    In your view controller, get the read-only value of self.interfaceOrientation (the current orientation of the interface).

    0 讨论(0)
  • 2020-12-02 08:20

    Orientation information isn't very consistent, and there are several approaches. If in a view controller, you can use the interfaceOrientation property. From other places you can call:

    [[UIDevice currentDevice] orientation]
    

    Alternatively, you can request to receive orientation change notifications:

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

    Some people also like to check the status bar orientation:

    [UIApplication sharedApplication].statusBarOrientation
    
    0 讨论(0)
  • 2020-12-02 08:21

    You can achieve this by two ways:

    1- By using the following method:

    **Put the following line in the -(void)viewDidLoad Method:

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

    then put this method inside your class

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

    The above method will check the orientation when the device will be rotated

    2- The second way is by inserting the following notification inside -(void)viewDidLoad

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

    then put the following method inside your class

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

    The above method will check the orientation of the status bar of the ipad or iPhone and according to it you make do your animation in the required orientation.

    0 讨论(0)
  • 2020-12-02 08:21

    I've tried many of the above methods, but nothing seemed to work 100% for me.

    My solution was to make an iVar called orientation of type UIInterfaceOrientation in the Root View Controller.

    - (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;
    
    }
    

    Then, any place where you need to check the orientation you can do something like this:

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

    There may still be a better way, but this seems to work 98% of the time (iOS5 notwithstanding) and isn't too hard. Note that iOS5 always launches iPad in portrait view, then sends a device the willRotateTo- and didRotateFromInterfaceOrientation: messages, so the value will still be inaccurate briefly.

    0 讨论(0)
  • 2020-12-02 08:22

    For determining landscape vs portrait, there is a built-in function:

    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
    BOOL inLandscape = UIDeviceOrientationIsLandscape(orientation);
    
    0 讨论(0)
提交回复
热议问题