首页 文章

如何在iPhone中纵向/横向锁定屏幕?

提问于
浏览
1

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

纵向/横向模式 .

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

一个是纵向文件,另一个是横向文件 .

我想只在纵向视图中打开纵向文档,而在横向文档中打开

仅景观 .

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

肖像文档所以它必须在纵向视图中旋转,如果相同,对于横向视图也是如此

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

必须旋转或必须仅以横向打开文档 .

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

需要你的帮助 .

先感谢您

这是我的一些代码:

- (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 回答

  • 2

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

    - (BOOL)shouldAutorotate {
        YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
    
      if (<PDF is portrait>)
        return UIInterfaceOrientationMaskPortrait;
      if (<PDF is landscape>)
        return UIInterfaceOrientationMaskLandscape;
    
      return UIInterfaceOrientationMaskAll;
    
    }
    

    希望这可以帮助 .

    编辑:

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

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
                                duration:(NSTimeInterval)duration {
    
        if (toOrientation == UIInterfaceOrientationMaskLandscape)
            pdfScrollViewFrame.transform = CGAffineTransformMakeRotation(M_PI/2);
    
        … // handle all other cases here
    }
    

    为了仅在 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>)
          return UIInterfaceOrientationMaskPortrait;
        if (<PDF is landscape>)
          return UIInterfaceOrientationMaskLandscape;
      }
      return UIInterfaceOrientationMaskAll;
    
    }
    …
    

相关问题