How to lock screen in portrait/landscape in iPhone

2019-01-26 01:54发布

问题:

My Application supports all orientation but in few cases i want to lock the screen in

portrait/landscape mode.

In my Application i have two kind of pdf document.

one is in portrait document and other is landscape document .

i want to open portrait document in portrait view only, and landscape document in

landscape view only.

i want to do like this: if my application is open in landscape view and i click on the

portrait document so it must rotate in portrait view and same like this for landscape if

my application is open in portrait view and when i click on the landscape document it

must be rotate or it must open the document in landscape only.

hope i make you guys clear pardon my english hope you understand what i want please

need your help .

Thank you in advance

here is my some code :

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

  if (orientation == UIInterfaceOrientationPortrait || orientation == 
         UIInterfaceOrientationPortraitUpsideDown) {

            NSLog(@"Portrait");
            if ([orientationObject isEqualToString:@"p"]) {
               //If the document is portrait
                pdfScrollViewFrame = CGRectMake(-0.0, -80.0, 770.0, 1085.0);
            }
            else{
                // If the document is landscape
                pdfScrollViewFrame = CGRectMake(-0.0, -40.0, 770.0, 1130.0);
            }
        }
        else{

           NSLog(@"Landscape");

            if ([orientationObject isEqualToString:@"p"]) {
             //If the document is portrait
               pdfScrollViewFrame = CGRectMake(65.0, -80.0, 620.0, 1110.0);
            }
            else{
                //if the document is landscape
                pdfScrollViewFrame = CGRectMake(0.0, -40.0, 740.0, 1070.0);
            }
        }

回答1:

For iOS6+, you could add this to your controller:

- (BOOL)shouldAutorotate {
    YES;
}

- (NSUInteger)supportedInterfaceOrientations {

  if (<PDF is portrait>)
    return UIInterfaceOrientationMaskPortrait;
  if (<PDF is landscape>)
    return UIInterfaceOrientationMaskLandscape;

  return UIInterfaceOrientationMaskAll;

}

Hope this helps.

EDIT:

I am not sure if you need to manually rotate the PDF to get the results you want. In this case you might try with something like:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
                            duration:(NSTimeInterval)duration {

    if (toOrientation == UIInterfaceOrientationMaskLandscape)
        pdfScrollViewFrame.transform = CGAffineTransformMakeRotation(M_PI/2);

    … // handle all other cases here
}

In order to only lock rotation after viewDidAppear, I would do following:

@interface…
…
   @property (nonatomic) BOOL isRotationLocked;
…
@end

@implementation…
…
- (void)viewDidAppear:(BOOL)animated {
  …
  self.isRotationLocked = YES;
}

- (BOOL)shouldAutorotate {
    YES;
}

- (NSUInteger)supportedInterfaceOrientations {

  if (self.isRotationLocked) {
    if (<PDF is portrait>)
      return UIInterfaceOrientationMaskPortrait;
    if (<PDF is landscape>)
      return UIInterfaceOrientationMaskLandscape;
  }
  return UIInterfaceOrientationMaskAll;

}
…