如何在iPhone中纵向/横向锁定屏幕? [英] How to lock screen in portrait/landscape in iPhone?

查看:311
本文介绍了如何在iPhone中纵向/横向锁定屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序支持所有方向,但在少数情况下我想锁定屏幕



纵向/横向模式。



在我的应用程序中,我有两种pdf文档。



一个是纵向文档,另一个是横向文档。



我想仅在纵向视图中打开纵向文档,并且仅在

横向视图中打开横向文档。



我想这样做:如果我的应用程序在横向视图中打开,我点击



纵向文档,因此它必须在纵向视图中旋转如果



我的应用程序在纵向视图中打开,当我点击横向文档时



必须旋转或者它必须只在横向打开文件。



希望我让你们明白原谅我的英语希望你明白我想要的东西



需要你的帮助。



提前谢谢



这是我的一些代码:

   - (void)viewDidLoad 
{
[super viewDidLoad];

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

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

NSLog(@Portrait);
if([orientationObject isEqualToString:@p]){
//如果文件是肖像
pdfScrollViewFrame = CGRectMake(-0.0,-80.0,770.0,1085.0);
}
else {
//如果文件是横向的
pdfScrollViewFrame = CGRectMake(-0.0,-40.0,770.0,1130.0);
}
}
else {

NSLog(@Landscape);

if([orientationObject isEqualToString:@p]){
//如果文件是肖像
pdfScrollViewFrame = CGRectMake(65.0,-80.0,620.0,1110.0);
}
else {
//如果文档是横向的
pdfScrollViewFrame = CGRectMake(0.0,-40.0,740.0,1070.0);
}
}


解决方案

iOS6 +,您可以将其添加到您的控制器:

   - (BOOL)shouldAutorotate {
YES;
}

- (NSUInteger)supportedInterfaceOrientations {

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

返回UIInterfaceOrientationMaskAll;

}

希望这有帮助。



编辑:



我不确定您是否需要手动旋转PDF以获得所需的结果。在这种情况下,您可以尝试使用以下内容:

   - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
duration: (NSTimeInterval)持续时间{

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

... //处理所有其他情况
}

为了仅在 viewDidAppear 之后锁定旋转,我会执行以下操作:

  @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>)
返回UIInterfaceOrientationMaskPortrait;
if(< PDF is landscape>)
返回UIInterfaceOrientationMaskLandscape;
}
返回UIInterfaceOrientationMaskAll;

}
...


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);
            }
        }

解决方案

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;

}
…

这篇关于如何在iPhone中纵向/横向锁定屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆