我正在开发一个iOS应用程序,我需要旋转6个UIViews,这样他们总是“面对”用户。接口的其余部分不需要旋转。
下面的代码在模拟器中工作得非常好。但是当我在手机上测试时遇到了问题。每当我将手机放在平坦的原始/水平时,它都会理解它应该回到肖像。知道这是怎么回事并且解决了吗?
#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;
}
答案 0 :(得分:2)
您的旋转角度默认值为0,并且未明确处理案例UIDeviceOrientationFaceUp。
或者,您可以拨打[[UIApplication sharedApplication] statusBarOrientation]
而不是[UIDevice currentDevice].orientation
。