你仍然可以使你的应用程序的支持只是肖像,旋转阻止,并赶上与加速度计的旋转动作。
下面是一些代码来执行此:
头文件:
@interface MyController : UIViewController <UIAccelerometerDelegate>
@property(nonatomic, assign) UIInterfaceOrientation interfaceOrientation;
@end
在实现文件:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration*)acceleration;
{
CGFloat x = -[acceleration x];
CGFloat y = [acceleration y];
CGFloat angle = atan2(y, x);
if (angle >= -2.25f && angle <= -0.25f)
{
self.interfaceOrientation = UIInterfaceOrientationPortrait;
}
else if (angle >= -1.75f && angle <= 0.75f)
{
self.interfaceOrientation = UIInterfaceOrientationLandscapeRight;
}
else if(angle >= 0.75f && angle <= 2.25f)
{
self.interfaceOrientation = UIInterfaceOrientationPortraitUpsideDown;
}
else if (angle <= -2.25f || angle >= 2.25f)
{
self.interfaceOrientation = UIInterfaceOrientationLandscapeLeft;
}
}
只记得在某个地方取消加速度计,就像在你viewWillDisappear:
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
}
请提供一些反馈,如果工作与否。