【问题标题】:How to fix orientation?如何确定方向?
【发布时间】:2017-12-13 19:49:04
【问题描述】:

我需要像 Youtube 的全屏一样修复方向纵向或横向。 当用户单击按钮时,它被更改为纵向或横向。 并被修复。 用户只能通过按钮进行控制。

一般接受设备方向纵向、横向左侧 这是我的代码

AppDelegate

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{
        if(restrictRotation)
            return UIInterfaceOrientationMaskPortrait;
        else
            return UIInterfaceOrientationMaskLandscapeLeft;
}

视图控制器

-(void) restrictRotation:(BOOL) restriction
{
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate->restrictRotation = restriction;
}

- (IBAction)rotateOrientationAction:(id)sender 
    {
        [self restrictRotation:NO];
        if(isPortrait){
            [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
            self.myNewScrollViewHeight.constant = self.view.frame.size.height - self.naviBar.frame.size.height - self.horizMenu.frame.size.height;
            self.portraitMenuView.hidden = YES;

        }else{

            [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
            self.myNewScrollViewHeight.constant = self.view.frame.size.height * 0.5;
            self.portraitMenuView.hidden = NO;
        }
        isPortrait = !isPortrait;
       [self restrictRotation:YES];
    }

如果我点击按钮,它会被更改为横向,但不会再次更改纵向。

谢谢

【问题讨论】:

  • 你尝试过什么,分享一下代码,你遇到了什么困难。
  • 编辑代码谢谢

标签: ios objective-c iphone


【解决方案1】:

restrictRotation 不满足您的条件,请检查以下代码以获取结果。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    isPortrait = YES;
}

-(void) restrictRotation:(BOOL) restriction
{
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;
}

- (IBAction)rotateOrientationAction:(id)sender
{
    if (isPortrait) {
        [self restrictRotation:NO];
        [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
    } else {
        [self restrictRotation:YES];
        [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
    }

    isPortrait = !isPortrait;
}

更新:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    isPortrait = YES;
}


- (IBAction)rotateOrientationAction:(id)sender
{
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = NO;
    [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];

    if (isPortrait)
    {
        [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
    }
    else
    {
        [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
    }

    isPortrait = !isPortrait;
}

【讨论】:

  • 感谢您的回答!它运作良好,但我发现了一个问题。如果我抓取设备反向(纵向状态 -> 抓取横向,横向状态 -> 抓取纵向),它不起作用。如果我控制方向。我应该抓住相同的方向。我为我糟糕的英语水平感到抱歉。谢谢
  • 设备在应用中的方向是纵向的。 硬件的方向是横向的。 -> 点击按钮 -> 它没有旋转
  • 你检查了吗? @양홍범
  • 是的。我检查了更新!但还是一样。所以去除了固定。我刚刚接受了自动旋转。谢谢您的回答!太感谢了!它对我很有帮助@iOS Dev
猜你喜欢
  • 1970-01-01
  • 2010-11-14
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多