[英]Check if current orientation is supported orientation or not
In my property list file, I have mentioned all the supported orientations. 在我的属性列表文件中,我提到了所有受支持的方向。 I have many applications which support different orientations.
我有许多支持不同方向的应用程序。 In order to handle all the UI related stuff with all these applications, I have a common project.
为了处理所有这些应用程序中与UI相关的所有内容,我有一个共同的项目。 So, I cannot do any app specific stuff in this common project as it would affect other apps too.
因此,我无法在此公共项目中执行任何应用程序特定的工作,因为它也会影响其他应用程序。 In one of files in this common project, I need to check if the device orientation is supported or not.
在此公共项目中的一个文件中,我需要检查是否支持设备方向。
I have retrieved the supported orientations in an array using 我已经使用数组检索了受支持的方向
NSArray *supportedOrientations = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"];
my method has signature 我的方法有签名
-(BOOL) isInvalidOrientation: (UIDeviceOrientation) orientation
I need to check if current orientation is supported or not, ie I nee to check if current orientation is there in supportedOrientations array or not. 我需要检查当前方向是否受支持,即需要检查supportedOrientations数组中是否存在当前方向。
I am unable to do that because when I use 我无法做到这一点,因为当我使用
[supportedOrientations containsObject:self.currentOrientation];
I get an error saying Implicit conversion of UIDeviceOrientation to id is disallowed with ARC. 我收到一条错误消息,指出不允许将UIDeviceOrientation隐式转换为id。
It is because they are incompatible types. 这是因为它们是不兼容的类型。 How do I check it ?
我该如何检查?
The problem is that the UISupportedInterfaceOrientations
info key gives you an array of strings. 问题是
UISupportedInterfaceOrientations
信息键为您提供了一个字符串数组。 While self.currentOrientation
gives you an enum value from UIDeviceOrientation
. 而
self.currentOrientation
为您提供UIDeviceOrientation
的枚举值。 You need a way to map the enum values to the string values. 您需要一种将枚举值映射到字符串值的方法。 Also note that you are dealing with device orientations and interface orientations.
另请注意,您正在处理设备方向和界面方向。
- (NSString *)deviceOrientationString:(UIDeviceOrientation)orientation {
switch (orientation) (
case UIDeviceOrientationPortrait:
return @"UIInterfaceOrientationPortrait";
case UIDeviceOrientationPortraitUpsideDown:
return @"UIInterfaceOrientationPortraitUpsideDown";
case UIDeviceOrientationLandscapeLeft:
return @"UIInterfaceOrientationLandscapeLeft";
case UIDeviceOrientationLandscapeRight:
return @"UIInterfaceOrientationLandscapeRight";
default:
return @"Invalid Interface Orientation";
}
}
NSString *name = [self deviceOrientationString:self.currentOrientation];
BOOL res = [supportedOrientations containsObject:name];
I have a similar need to determine the app supported orientations in order to pre cache some resources. 我有类似的需要确定应用程序支持的方向,以便预先缓存一些资源。 But I only need to know if the app supports portrait, landscape or both.
但是我只需要知道该应用程序是否支持纵向,横向或同时支持纵向和横向。 This thread led me to the following solution so I thought I might post it.
该线程将我引向以下解决方案,所以我认为我可以发布它。
// get supported screen orientations
NSArray *supportedOrientations = [[[NSBundle mainBundle] infoDictionary]
objectForKey:@"UISupportedInterfaceOrientations"];
NSString *supportedOrientationsStr = [supportedOrientations componentsJoinedByString:@" "];
NSRange range = [supportedOrientationsStr rangeOfString:@"Portrait"];
if ( range.location != NSNotFound )
{
NSLog(@"supports portrait");
}
range = [supportedOrientationsStr rangeOfString:@"Landscape"];
if ( range.location != NSNotFound )
{
NSLog(@"supports landscape");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.