Objective c 如何让iPhone获得最新定位?
有没有一种特殊的方法来定位iPhone?我不需要它的度数或弧度,我希望它返回一个UIInterfaceOrientation对象。我只是想用它来做一个if-else结构Objective c 如何让iPhone获得最新定位?,objective-c,accelerometer,Objective C,Accelerometer,有没有一种特殊的方法来定位iPhone?我不需要它的度数或弧度,我希望它返回一个UIInterfaceOrientation对象。我只是想用它来做一个if-else结构 if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) { //Code } if (currentOrientation==UIInterfac
if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//Code
}
if (currentOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft ) {
//Code
}
提前谢谢 这很可能是您想要的:
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
然后可以使用系统宏,如:
if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
}
如果需要设备方向,请使用:
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
这包括像
UIDeviceOrientationFaceUp
和UIDeviceOrientationFaceDown
这样的枚举,正如在其他答案中所讨论的,您需要的是interfaceOrientation,而不是deviceOrientation
最简单的方法是在UIViewController上使用属性interfaceOrientation。(所以大多数情况下,只要:self.interface定向就可以了)
可能的值为:
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
记住:
通过将设备向右旋转来输入左方向。以下是我需要编写的代码片段,因为当方向更改时,我的根视图中出现了一些wierd问题。。。。我知道你只是调用了应该调用的方法,但似乎是。。。。这很有效,没有错误
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){
//do something or rather
[self
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
NSLog(@"landscape left");
}
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){
//do something or rather
[self
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
NSLog(@"landscape right");
}
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){
//do something or rather
[self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
NSLog(@"portrait");
}
}
UIInterfaceOrientation
现在已被弃用,UIDeviceOrientation
包括UIDeviceOrientationFaceUp
和UIDeviceOrientationFaceDown
,因此不能依赖它们来为您提供接口方向
不过,解决方案相当简单
if (CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds)) {
// Landscape
} else {
// Portrait
}
随着
[UIApplication statusBarOrientation]
被弃用,您现在应该使用:
UIWindowScene *activeWindow = (UIWindowScene *)[[[UIApplication sharedApplication] windows] firstObject];
UIInterfaceOrientation orientation = [activeWindow interfaceOrientation] ?: UIInterfaceOrientationPortrait;
这在技术上是错误的,您从方法调用中获取UIDeviceOrientation并将其存储在UIInterfaceOrientation中。UIInterfaceOrientation是4个接口之一,它表示您的接口的外观。UIDeviceOrientation是6个选项中的一个(包括平铺和倒置),它告诉您设备的方向,而不一定是您的界面外观。您的界面可能处于纵向模式,但如果设备位于桌子上,它将返回UIDeviceOrientationFaceUp。@CoryImdieke:感谢您更新设备和界面方向之间的差异。UIInterfaceOrientation currentOrientation=[[UIApplication sharedApplication]statusBarOrientation];正确吗?这在技术上是错误的。我同意@Cory的观点,但效果很好。我将在下面发布一种方法,该方法不会给出错误,并且是相同的概念。当您不希望界面改变方向,但同时您希望方向反馈而不必使用陀螺仪时,这种方法非常有效。
statusBarOrientation
现在已被弃用。太糟糕了,它现在已被弃用。是的,这是弃用的,我们现在该怎么办?这不适用于UIDeviceOrientationFaceUp
和UIDeviceOrientationFaceDown