我已经研究了一段时间,并用谷歌搜索了我能想到的所有相关内容。
我的应用中有一个按钮可以切换旋转。当我关闭旋转时,旋转设备,然后重新打开旋转,我需要一种方法将应用方向与设备方向相匹配。
@interface ViewController ()
{
BOOL _rotationStatus;
}
按下按钮的方法:
-(void)toggleRotation
{
if(_rotationStatus == NO)
{
_rotationStatus = YES;
}
else
{
_rotationStatus = NO;
}
}
- (BOOL)shouldAutorotate //only targeting iOS 6
{
return !_rotationStatus;
}
这非常适合切换旋转,但是当重新打开旋转时,在您再次旋转设备之前,方向不会“刷新”以匹配设备方向。
例如
所需:App旋转到横向以匹配设备。
实际:应用程序保持纵向状态,直到收到另一个轮换通知。
我认为纠正这种情况的方法是检查设备方向,检查应用方向并在旋转切换回来时匹配它们。
我将此添加到方法中:
if ((UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) && ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait))
{
NSLog(@"mis-matched Device and App orientations");
//do the rotation?
[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:YES];
}
所以完整的方法看起来像这样。
-(void)toggleRotation
{
if(_rotationStatus == NO)
{
_rotationStatus = YES;
}
else
{
_rotationStatus = NO;
if ((UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) && ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait))
{
NSLog(@"mis-matched Device and App orientations");
//do the rotation?
[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:YES];
}
}
}
NSLog会在我预期的时候打印,但是对setStatusBarOrientation的调用不会导致旋转。
我是iOS新秀,所以我确信我做错了很多事。有一个更好的方法吗?