【问题标题】:How to change the orientation of an device depending upon switch button ON & OFF?如何根据开关按钮的开和关来改变设备的方向?
【发布时间】:2016-04-20 10:33:41
【问题描述】:

我需要在应用运行时更改应用方向。我的过程是,如果我打开开关,应用程序应更改其方向(即)(i)如果应用程序以横向运行,然后如果我打开“打开”开关按钮,则设备方向应自动更改为纵向模式。 . (ii) 如果应用程序在纵向模式下运行,如果我关闭开关,那么设备方向应该会自动变为横向模式......我已经尝试过一些东西,但它对我来说不起作用......任何人都可以提供一些解决方案为此..

 - (IBAction)PortraitSwitchPressed:(UISwitch *)sender
   {
     if (sender.isOn)
     {
        UIInterfaceOrientationMaskPortrait;
        [[UIDevice currentDevice]setOrientation:UIInterfaceOrientationPortrait]; // no visible @interface for 'UIDevice' declares the selector setorientation
          [self.PortraitSwitch setOn:YES animated:YES];
      }
else
     {

       UIInterfaceOrientationMaskLandscape;
      [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationMaskLandscape];
         [self.PortraitSwitch setOn:NO animated:YES];
}}

【问题讨论】:

    标签: ios objective-c uiinterfaceorientation


    【解决方案1】:

    试试下面的代码:

    - (IBAction)PortraitSwitchPressed:(UISwitch *)sender
    {
            if (sender.isOn)
            {
                NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
                [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
                [self.PortraitSwitch setOn:YES animated:YES];
            }
            else
            {
    
                NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];//or UIInterfaceOrientationLandscapeRight
                [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
                [self.PortraitSwitch setOn:NO animated:YES];
            }
    }
    

    根据您的要求对UIInterfaceOrientation 进行修改。

    【讨论】:

    • 它在场景 1 中运行良好.. 当应用程序处于横向模式并且如果我打开 Switch 时,应用程序方向正在变为纵向模式.. 这很好.. 但在场景 2 中它失败了..当应用程序处于纵向模式时,开关处于开启阶段..如果我尝试关闭开关,则方向不会变为横向模式
    • 通过UIInterfaceOrientationLandscapeLeftUIInterfaceOrientationLandscapeRight 尝试。
    • 太棒了......谢谢你 Ronak Chaniyara.. 它正在工作
    • 你很高兴...很乐意提供帮助!@Nithya
    猜你喜欢
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 2016-02-22
    • 2013-02-05
    • 1970-01-01
    • 2022-06-24
    • 2016-02-01
    • 2014-02-16
    相关资源
    最近更新 更多