简体   繁体   English

定向问题

[英]issue with orientation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
    {
        [self isPortraitSplash];
    }
    else if ([[UIDevice currentDevice] orientation] == UIInterfaceOrientationIsLandscape(interfaceOrientation))
    {
        [self isLandScapeSplash];
    }
    return  YES;
}  

In my methods isPortraitSplash and isLandScapeSplash , i am setting the frames for the view. 在我的方法isPortraitSplashisLandScapeSplash ,我正在设置视图的框架。

When orientation changes, it's always calling isLandScapeSplash - not able to call isPortraitSplash method. 当方向发生变化时,它始终会调用isLandScapeSplash无法调用isPortraitSplash方法。

Can any one advise me why this is happening? 谁能告诉我为什么会这样?

Your existing if statement is comparing a BOOL to a UIDeviceOrientation . 您现有的if语句正在将BOOLUIDeviceOrientation进行比较。 Your test needs to be: 您的测试必须是:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
    {
        [self isPotraitSplash];
    }
    else if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
    {
        [self islandScapeSplash];
    }
    return  YES;
}  

UIInterfaceOrientationIsPortrait returns a BOOL , so that's all you need in your if statement condition. UIInterfaceOrientationIsPortrait 返回一个BOOL ,这就是您在if语句条件中所需的全部。

Update: I will also add that I agree with the other answers that it's better to do this work in willRotateToInterfaceOrientation:duration: , instead of shouldAutorotateToInterfaceOrientation: . 更新:我还要补充一点,我同意其他答案,那就是最好在willRotateToInterfaceOrientation:duration:进行此工作,而不是在willRotateToInterfaceOrientation:duration: shouldAutorotateToInterfaceOrientation:

But, that isn't the reason your original code was failing. 但这不是您原始代码失败的原因。 The original code was failing because of the if test comparing the UIDeviceOrientation to the BOOL . 由于if测试将UIDeviceOrientationBOOL进行了比较,因此原始代码失败。

Use - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration instead of shouldAutorotateToInterfaceOrientation , it is guaranteed to be called before a rotation occurs. 使用- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration ,而不是shouldAutorotateToInterfaceOrientation ,这是保证一个旋转发生之前被调用。

Do not remove shouldAutorotateToInterfaceOrientation , return YES for every orientation you want to support. 不要删除shouldAutorotateToInterfaceOrientation ,对要支持的每个方向返回YES。

First of all in 首先在

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

you have to declare which all orientations you want to support. 您必须声明要支持的所有方向。

and in 和在

- (BOOL)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) 
    {
        [self isPotraitSplash];
    }
    else if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
    {
        [self islandScapeSplash];
    }
}

you have to set the frames or any other for layout changes, and use like above. 您必须设置框架或任何其他布局更改,并像上面一样使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM