Get current orientation of iPad?

后端 未结 11 1712
野趣味
野趣味 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: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.

提交回复
热议问题