Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
813 views
in Technique[技术] by (71.8m points)

ios - How to lock screen in portrait/landscape in iPhone?

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);
            }
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

}
…

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...