开发者

Shouldautorotate returning wrong value

开发者 https://www.devze.com 2023-01-20 14:38 出处:网络
I just tried adding some print statements to my shouldautorotate method and noticed that it checks it 4 times which does make sense but even though I am not switching mode from portrait to landscape,

I just tried adding some print statements to my shouldautorotate method and noticed that it checks it 4 times which does make sense but even though I am not switching mode from portrait to landscape,

it returns portrait 3 tim开发者_运维技巧es and on the fourth time, it returns landscape even though my simulator is not in landscape.

if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
   NSLog(@"landscape left");
  }else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
   NSLog(@"landscape right");
  }else if(interfaceOrientation == UIInterfaceOrientationPortrait){
   NSLog(@" portrait");
  }else if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
   NSLog(@"portrait upsidedown");
  }

Any one knows why?


Try putting that code into the didAutorotate or the willAutorotate method. shouldAutorotate is only supposed to return YES or NO.

I'm theorising that shouldAutorotate is checked regularly, whereas didAutorotate is only fired of when it detects an orientation shift.

This is the code I use to check:

- (void) reOrient{
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){  

    } else {

    }
}

That is in a method I created called reOrient which is called from my didAutorotate

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [self reOrient];
}

Just make sure when you create a new method like reOrient that you declare it in the header as well (I kept forgetting when I was starting out) as below:

- (void)reOrient;
0

精彩评论

暂无评论...
验证码 换一张
取 消