1

I would like to make a custom orientation lock button for a reader app of mine, and I was thinking it wouldn't be too bad to whip up, but alas I am the one getting whipped.

To start off I do have this method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
 }

And then I was thinking that I could handle the actual locking in an action method like this:

- (IBAction) screenLock:(id)sender{

if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait){

    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

}else{

            [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

}

  }

But alas, this code will not hold sway over the former that instructs the view to rotate...

Am I going about this all wrong? What is a better way to do it? I just want to have local, easy way to have my users lock the orientation of their screen. I guess it would be using a boolean value where they hit a button to lock and then hit again to unlock...

Thoughts? Thanks!!

Jeffrey H.
  • 81
  • 1
  • 3

1 Answers1

3

shouldAutorotateToInterfaceOrientation ripples up your view hierarchy so your logic needs to be put into your app delegate (or as the most senior ViewController that might return YES). Put a BOOL property in your appDelegate and set it via your lock button (e.g. target pointers/delegates (AppDelegate)) then in your appDelegate do something like this:

#define ROTATION_MASTER_ENABLED 1

//Setting MASTER_ROTATION_LOCK_ENABLED to 0 will stop the device rotating
//Landscape UP>landscape DOWN and Portrait UP>Portrait DOWN, 
//This is not generally desired or app store safe, default = 1

-(BOOL)compareOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    UIInterfaceOrientation actual = [[UIDevice currentDevice] orientation]; 
    if(UIInterfaceOrientationIsLandscape(interfaceOrientation) && UIInterfaceOrientationIsLandscape(actual))return YES; 
    else if(UIInterfaceOrientationIsPortrait(interfaceOrientation)&& UIInterfaceOrientationIsPortrait(actual))return YES;
    else return NO;   

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{   
    if(!MASTER_ROTATION_LOCK_ENABLED)return NO;
    else if(self.rotationEnabled || [self compareOrientation:interfaceOrientation])return YES;
    return NO;//self.rotationEnabled is a BOOL
}
Community
  • 1
  • 1
Luke Mcneice
  • 3,012
  • 4
  • 38
  • 50
  • 1
    No. This method should return YES for the supported orientations - which in your case returns either always YES or always NO. – Eiko Dec 07 '10 at 16:31
  • @Eiko, the way it's currently written will allow the view controller to rotate to any orientation (by returning YES if the lock is not enabled) and prevent it from rotating at all if the lock is enabled. I don't see why this deserves a downvote? – Jasarien Dec 07 '10 at 16:46
  • Hmm got me thinking and I was probably a bit quick... so take back ;-) – Eiko Dec 07 '10 at 16:50
  • Wow, thanks for the great insights everyone, especially Luke for the answer. Sorry if this is trivial, but how might I go about accessing a variable in the app delegate from one of the other views? I just don't have any experience with that yet. Thanks!! – Jeffrey H. Dec 07 '10 at 16:59
  • @Jeffrey H. You might want to ask it in another question, but I'll post something here (sorry about the formatting). yourDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSLog(appDelegate.hello); where "hello" is a NSString in your delegate. – sudo rm -rf Dec 07 '10 at 21:05
  • @sudo rm you can still `code` format text with backticks in the comments – Luke Mcneice Dec 08 '10 at 10:06