如何确定方向?

时间:2017-07-10 07:17:45

标签: ios objective-c iphone

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

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

的AppDelegate

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

的ViewController

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

如果我点击按钮,它会更改landscapeLeft但不会再次更改肖像。

谢谢

1 个答案:

答案 0 :(得分: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;
}