我的模态视图不会旋转

时间:2012-02-29 21:17:04

标签: ios view orientation modal-dialog

我遇到了模态视图显示的问题。我的第一个视图旋转得很好。模态视图的控制器实现“ShouldAutorotationToInterfaceRotation”方法,但是当我旋转设备或模拟器时,模态视图不会旋转。

我添加了一些注释来检查第一个视图是否检测到旋转事件而不是模态视图,但是没有,它没有得到事件。

你有线索吗?

1 个答案:

答案 0 :(得分:1)

您需要订阅NSNotificationCenter以接收模态视图的旋转事件。然后,如果要保持模态视图相对于底部视图的正确定位,一种解决方案是转换视图。这是一个实现:

将其放在 viewDidLoad 中并将原始方向保存为角度:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
                                             name:UIDeviceOrientationDidChangeNotification object:nil];

int initialOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (initialOrientation == UIDeviceOrientationPortrait)
    initialAngle = 0.0;
else if (initialOrientation == UIDeviceOrientationLandscapeRight)
    initialAngle = 90.0;
else if (initialOrientation == UIDeviceOrientationPortraitUpsideDown)
    initialAngle = 180.0;
else if (initialOrientation == UIDeviceOrientationLandscapeLeft)
    initialAngle = 270.0;

currentAngle = initialAngle;

然后使用代码定义函数 orientationChanged:(NSNotification *)通知,以便相对于初始方向正确旋转:

int newOrientation = [[notification object] orientation];
if (newOrientation == UIDeviceOrientationPortrait)
    desiredAngle = 0.0;
else if (newOrientation == UIDeviceOrientationLandscapeRight)
    desiredAngle = 90.0;
else if (newOrientation == UIDeviceOrientationPortraitUpsideDown)
    desiredAngle = 180.0;
else if (newOrientation == UIDeviceOrientationLandscapeLeft)
    desiredAngle = 270.0;

if(desiredAngle != currentAngle)
{
    CGAffineTransform rotate = CGAffineTransformMakeRotation(M_PI * (-desiredAngle+initialAngle) / 180.0);    
    CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 0);
    [[self releaseView] setTransform:CGAffineTransformConcat(translate, rotate)];
}
currentAngle = desiredAngle;