【问题标题】:How to lock screen in portrait/landscape in iPhone?如何在 iPhone 中以纵向/横向锁定屏幕?
【发布时间】:2013-12-16 05:22:46
【问题描述】:

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

纵向/横向模式。

在我的应用程序中,我有两种 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);
            }
        }

【问题讨论】:

标签: ios objective-c orientation lockscreen landscape-portrait


【解决方案1】:

对于 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;

}
…

【讨论】:

  • 我在我的应用程序中使用你最新的 github UIPageViewController-PDF 项目,在你的项目中,我试图处理这么多天的方向,你知道我一直在问你再次请帮助我。
  • 实际上我想要的是,假设我的应用程序以横向视图(模式)打开,当我点击打开我的纵向 Pdf 文档时,下一个视图(第二个屏幕)它必须旋转屏幕人像模式。我的应用程序支持所有方向,但在打开文档时我想检查文档是否是纵向的,因此控制器应该纵向旋转,或者如果我单击横向文档并且我的应用程序以纵向运行,所以它应该改变横向希望的方向你明白我想说什么:(
  • 其实我是这么理解的。你试过我的代码吗?如果是这样,您能否检查您的应用是否进入了supportedInterfaceOrientations 以及它在那里做了什么?
  • 是的,我尝试了您的代码并在横向模式下打开我的纵向文档,因此它采用supportedInterfaceOrientations 方法,并且如果()返回UIInterfaceOrientationMaskPortrait;它也出现在此处,但不会纵向旋转屏幕。
  • 您是否删除了viewDidLoad 中的设置pdfScrollViewFrame?那应该不再需要了……除此之外,您是否会检查您的willRotateToInterfaceOrientation是否被调用以及使用哪些参数?如果你使用我刚刚提供的willRotateToInterfaceOrientation 的实现会发生什么?
猜你喜欢
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多