UIDevice Orientation

前端 未结 8 1518
忘了有多久
忘了有多久 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:26

    I recommend you to use my highlighted code instead of yours to safe some code of lines.

    -(void) viewDidLoad
    {
        [super viewDidLoad];
        [self rotations];
    }
    
    -(void)rotations
    {
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationChanged:)
                                             name:UIDeviceOrientationDidChangeNotification
                                             object:nil];
    }
    
    -(void) orientationChanged:(NSNotification *)notification
    {
        //USE THIS PART
        //USE THIS PART
        //USE THIS PART
        //USE THIS PART
        //USE THIS PART
        if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
        {
        }
    }
    

    INSTEAD OF

    if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait || 
       [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortraitUpsideDown) 
    {
    }
    
    0 讨论(0)
  • 2020-12-13 08:33

    Another way of doing this without turning on orientation notification would be to

    Step 1: Save the current orientation in a local variable myCurrentOrientation and assign it like this:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                    duration:(NSTimeInterval)duration
    {
        myCurrentOrientation = toInterfaceOrientation;
    }
    

    Step 2: Use myCurrentOrientation for your check

    if (UIInterfaceOrientationIsLandscape(myCurrentOrientation) == YES) {
        // landscape
    }
    else {
        // portrait.
    }
    
    0 讨论(0)
提交回复
热议问题