【问题标题】:iOS6 Device Rotation To Be RestrictediOS6 设备轮换受到限制
【发布时间】:2012-11-27 20:48:08
【问题描述】:

对于我的应用,我想让设备旋转,但要上下颠倒。这工作正常。但是,我想阻止应用程序专门从

旋转

横向左侧 -> 右侧横向 - 反之亦然

如果有人好奇,这是因为旋转会打乱我的布局,因为它们每个都从一个公共点旋转

我认为可行的 iOS 5 代码如下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

    NSLog(@"Rotating");
    if((lastOrient == 3 && toInterfaceOrientation == 4) || (lastOrient == 4 && toInterfaceOrientation == 3)){
       lastOrient = toInterfaceOrientation;
       return NO;
    }

   lastOrient = toInterfaceOrientation;
   return YES;

}

其中 3= 左侧横向,4= 右侧横向

关于如何使用 iOS6 执行此操作有什么建议吗?还是完全不同的解决方案?

【问题讨论】:

    标签: iphone ios ios6 autorotate


    【解决方案1】:

    shouldAutorotateToInterfaceOrientation 在 ios6 中已弃用。使用这个:

    - (BOOL)shouldAutorotate {
    
    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
    
    if (lastOrientation==UIInterfaceOrientationPortrait && orientation == UIInterfaceOrientationPortrait) {
     return NO;
    
    }
    
    return YES;
    }
    

    尚未测试此代码。您可以获得有关这些帖子的更多信息: shouldAutorotateToInterfaceOrientation is not working in iOS 6 shouldAutorotateToInterfaceOrientation not being called in iOS 6

    【讨论】:

    • 显然你可以同时包含两个实现(shouldAutorotateToOrientation 和 shouldAutorotate),它应该从 ios6 级联下来
    【解决方案2】:

    好的,我在这里回答了我自己的问题:

    好消息是,绝对有办法做到这一点!所以这是基础知识:

    在 iOS6 中,一般由 appDelegate 来处理应用是否可以旋转。然后,当设备收到旋转信号时,它会询问您的视图以了解其支持的方向。这是我实现我的代码的地方。实际上,shouldAutorotate() 在解决方案中没有任何作用。

    所以我创建了一个变量来跟踪最后的方向,并改变它

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

    这样我可以比较方向

    -(NSUInteger)supportedInterfaceOrientations
    {
        NSLog(@"Last Orient = %d", lastOrient);
        NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
    
        if (lastOrient != 3 && lastOrient != 4) {
            NSLog(@"All good, rotate anywhere");
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
        else if(lastOrient == 3){
            orientations |= UIInterfaceOrientationMaskLandscapeRight;
            NSLog(@"Can only rotate right");
        }
        else if(lastOrient == 4){
            orientations |= UIInterfaceOrientationMaskLandscapeLeft;
            NSLog(@"Can only rotate left");
        }
    
        return orientations;
    }
    

    似乎对我有用。有点 hack,但它做了它需要做的事情

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 2023-04-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多