我的应用程序在两个方向(纵向和横向)中都有效,但其中一个屏幕在纵向模式下被锁定。但我必须在该特定屏幕的Rotation变量中设置一个值。但我没有找到方向。 所以我想找到方向。 我使用以下代码在纵向模式下锁定我的屏幕,它可以正常工作。
- (UIInterfaceOrientationMask) supportedInterfaceOrientations {
[super supportedInterfaceOrientations];
return UIInterfaceOrientationMaskPortrait;
}
我使用以下方法检测方向,但不会调用此方法。
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(@"UIInterfaceOrientationPortrait");
}
else
{
NSLog(@"UIInterfaceOrientationland");
}
}
答案 0 :(得分:1)
如果屏幕在纵向模式下被锁定,您可以检测方向。简单的方法是“CoreMotion”。代码段如下所示。
1-)首先,您应该将CoreMotion freamework添加到Build Settings
2-)导入CoreMotion freamework
#import <CoreMotion/CoreMotion.h>
3-)添加属性如下。
@interface BaseGeneralViewController (){
UIInterfaceOrientation orientationLast, orientationAfterProcess;
CMMotionManager *motionManager;
UIInterfaceOrientation orientationNew;
}
4-)我们创建了初始化方法
- (void)initializeMotionManager{
motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = .2;
motionManager.gyroUpdateInterval = .2;
[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (!error) {
[self outputAccelerationData:accelerometerData.acceleration];
}
else{
NSLog(@"%@", error);
}
}];
}
5-)声明outputAccelerationData方法
- (void)outputAccelerationData:(CMAcceleration)acceleration{
if (acceleration.x >= 0.75 && orientationLast != UIInterfaceOrientationLandscapeLeft) {
orientationNew = UIInterfaceOrientationLandscapeLeft;
[self callQRCodePayment:UIDeviceOrientationLandscapeRight];
}
else if (acceleration.x <= -0.75 && orientationLast != UIInterfaceOrientationLandscapeRight) {
orientationNew = UIInterfaceOrientationLandscapeRight;
[self callQRCodePayment:UIDeviceOrientationLandscapeLeft];
}
else if (acceleration.y <= -0.75) {
//orientationNew = UIInterfaceOrientationPortrait;
//NSLog(@"Portrait");
}
else if (acceleration.y >= 0.75) {
//orientationNew = UIInterfaceOrientationPortraitUpsideDown;
}
else {
// Consider same as last time
return;
}
if (orientationNew == orientationLast)
return;
orientationLast = orientationNew;
}
6-)在viewDidload中调用initialize方法
- (void)viewDidLoad
{
[super viewDidLoad];
[self initializeMotionManager];
}
你可以看到帖子change Oriantation
的详细信息答案 1 :(得分:-1)
这里有检测方向UIDeviceOrientationIsLandscape
和UIDeviceOrientationIsPortrait
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
// code here for landscape orientation
}
// And
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
// code here for Portrait orientation
}