ios - 如何确定方向?

标签 ios objective-c iphone

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

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

应用委托(delegate)

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

View Controller

-(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];
    }

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

谢谢

最佳答案

您的条件不满足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 - 如何确定方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45005909/

相关文章:

ios - 在 iOS 设备的主屏幕上设置书签的默认标题?

ios - Objective-C iOS 类似雷达的 map

ios - Xcode 4.2或4.6的iOS 7部署

ios - 使用来自另一个 View 的特定索引的图像启动 UIScrollView

ios - 月份和年份的 UIPicker。动态获取年份

iphone - 获取 nsstring 的实例 ID

ios - bundle 标识符名称选择

iphone - 加快 iOS 中的标签文本更改

iphone - 更改 UITabBar 选定选项卡

ios - 如何通过 View 设置默认 UITextField 值已加载