0

我正在开发一个 iOS 应用程序,我需要在其中旋转 6 个 UIView,以便它们始终“面向”用户。界面的其余部分不需要旋转。

下面的代码在模拟器中运行得非常好。但是,当我在手机上进行测试时,我遇到了问题。每当我将手机放在平面起始/水平位置时,它都知道它应该回到纵向。知道这是怎么回事和解决方法吗?

#pragma mark Rotation
- (BOOL)shouldAutorotate
{
    return NO;
}

- (void)orientationChanged:(NSNotification *)notification
{
    CGFloat rotationAngle = [self convertOrientationToRadians];

    [UIView animateWithDuration:0.5 animations:^{
        for (TimerButton *timerButton in self.timerButtons) {
            timerButton.button.transform = CGAffineTransformMakeRotation(rotationAngle);
        }
    }];
}

- (CGFloat)convertOrientationToRadians
{
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;

    CGFloat rotationAngle = 0.0;

    switch (orientation) {
        case UIDeviceOrientationPortrait:
            rotationAngle = 0.0;
            break;
        case UIDeviceOrientationLandscapeLeft:
            rotationAngle = M_PI / 2;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            rotationAngle = M_PI;
            break;
        case UIDeviceOrientationLandscapeRight:
            rotationAngle = - M_PI / 2;
            break;
        default:
            break;
    }

    return rotationAngle;
}
4

1 回答 1

2

旋转角度的默认值为 0,并且未明确处理 UIDeviceOrientationFaceUp 的情况。

或者,您可以调用 [[UIApplication sharedApplication] statusBarOrientation]而不是[UIDevice currentDevice].orientation.

于 2013-08-26T16:18:56.270 回答