UIDevice Orientation

前端 未结 8 1517
忘了有多久
忘了有多久 2020-12-13 07:34

I have the following code in a method. When I run this in the simulator the debugger skips right over the code?? What am I missing?

if (([[UIDevice currentDe         


        
相关标签:
8条回答
  • 2020-12-13 08:07

    Note that there's a macro UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait, so instead of comparing it separately to LandscapeLeft and LandscapeRight you could just do it like this:

    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
    {
    }
    
    0 讨论(0)
  • 2020-12-13 08:11

    Here is a method to find the orientation and the true center of the screen. I used Tuszy's method so I could set UIActivityIndicatorView properly.

    - (BOOL) isPortraitOrientation {
        UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
        if(orientation == UIInterfaceOrientationPortrait ||
           orientation == UIInterfaceOrientationPortraitUpsideDown) {
            return true;
        }
        if(orientation == UIInterfaceOrientationLandscapeRight ||
           orientation == UIInterfaceOrientationLandscapeLeft) {
            return false;
        }
        return false;
    }
    

    And the way to get center...

    - (void) findMyUIViewCenter {
        CGPoint myCenter;
        if ([self isPortraitOrientation]) {
            myCenter = self.view.center;
        }
        else {
            myCenter = CGPointMake(self.view.frame.size.height / 2.0, self.view.frame.size.width / 2.0);
        }
        NSLog(@"true center -- x:%f y:%f )",myCenter.x,myCenter.y);
    }
    
    0 讨论(0)
  • 2020-12-13 08:17

    Update 2

    This shouldn't matter, but try turning on orientation notifications:

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

    Update

    My bad, I assumed it was empty.

    Try removing the or statement and just test for a single orientation. See if that fixes it. Maybe there is a bracket problem or something silly.

    I have the following test working in production code, so your technique should work:

        if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
            ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)) {
    
    
    }
    

    Original Answer

    You have to actually put statements in the if blocks to get it to step in.

    The debugger is smart enough to skip over empty blocks.

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

    The best way to determine interface orientation is to look at status bar orientation:

     UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    
        if(orientation == UIInterfaceOrientationPortrait || 
           orientation == UIInterfaceOrientationPortraitUpsideDown) {
    
           //Portrait orientation
    
    }
    
    if(orientation == UIInterfaceOrientationLandscapeRight ||
       orientation == UIInterfaceOrientationLandscapeLeft) {
    
        //Landscape orientation
    
    }
    

    UIDevice class measures orientation based on accelerometer and if device lays flat, it won't return the correct orientation.

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

    Heh you need to call [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications] before obtaining the value. Have a look at documentation of this method. Took me a while to track this down.

    0 讨论(0)
  • 2020-12-13 08:23

    Say you are inside a Springboard tweak and want to show something depending on the orientation of the current app, then you can use this (jailbreak only):

    UIInterfaceOrientation o = [[UIApplication sharedApplication] _frontMostAppOrientation];
    
    0 讨论(0)
提交回复
热议问题