您仍然可以使您的应用程序仅支持纵向,防止旋转,并使用加速度计捕捉旋转动作。
这是一些执行此操作的代码:
头文件:
@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];
}
请给一些反馈,如果工作与否。