我知道您可以使用UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
的if语句选择ipad或iphone。但我想知道你是否可以为iPhone 3.5英寸屏幕和iPhone 4英寸屏幕做同样的事情。我有一款最初设计用于iPhone 4英寸屏幕的游戏,但是自动布局已用于修复两个设备之间的错误,我需要改变的一件事就是当
if (ball.center.y > 600) {
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 36;
ball.center = CGPointMake(RandomPosition, -22);
}
因为它最初是在4英寸屏幕上设计的,所以游戏设置为当球>>时将球重置到屏幕顶部。 600像素,正好在iphone 4英寸屏幕下方。它仍能正常工作,3.5英寸屏幕底部和位置重置之间只有一点延迟。无论如何我可以用UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom(iphone4???)
建立一个if语句来设置一个新的ball.center.y> 300还是什么?
答案 0 :(得分:17)
您可以尝试以下代码。 在Constant.h中添加此代码
#define IS_IPAD (( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) ? YES : NO)
#define IS_IPHONE_5 (([UIScreen mainScreen].scale == 2.f && [UIScreen mainScreen].bounds.size.height == 568)?YES:NO)
#define IS_RETINA_DISPLAY_DEVICE (([UIScreen mainScreen].scale == 2.f)?YES:NO)
现在检查设备大小,在视图中添加以下代码
if (IS_IPAD)
{
//do stuff for iPad
}
else
{
if(IS_IPHONE_5)
{
//do stuff for 4 inch iPhone screen
}
else
{
//do stuff for 3.5 inch iPhone screen
}
}
答案 1 :(得分:6)
您可以通过以下声明检查3.5英寸和4英寸
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height>480.0f)
{
NSLog(@"App is running on iPhone with screen 4 inch");
}
else
{
NSLog(@"App is running on iPhone with screen 3.5 inch");
}
}
答案 2 :(得分:5)
根据其他答案,如果您计划进行大量检查,可能需要为它创建一个宏:
#define isLargerPhone() (([UIScreen mainScreen].bounds.size.height > 480) && [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
将其放入MyMacros.h
(或类似)中,您可以将其导入Prefix.pch
文件,然后只要您需要执行检查,就可以执行以下操作:
if ( isLargePhone()){
// do whatever...
}
答案 3 :(得分:2)
您可以使用以下方法检查iPhone是否为iPhone 5,即4英寸
- (BOOL)hasFourInchDisplay {
return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0);
}
答案 4 :(得分:1)
使用此方法了解设备类型和型号,如iPhone5 / 4S / 4或其他
+ (NSString *)yesButWhichDeviceIsIt
{
BOOL hasRetina = NO;
if ([UIScreen instancesRespondToSelector:@selector(scale)])
{
CGFloat scale = [[UIScreen mainScreen] scale];
if (scale > 1.0)
{
hasRetina = YES;
}
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
if (hasRetina)
{
return @"iPad retina";
} else {
return @"iPad";
}
}
else {
if (hasRetina)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
return @"iPhone5"; //4 inch 64 bit
}
else {
return @"iPhone4s"; // 4 inch
}
}
else {
return @"iPhone"; //3.5 inch
}
}
}
我希望这会有所帮助
答案 5 :(得分:0)
好的我在这里它属于一个类别,很抱歉作为答案,但它允许我更好的格式化...
第一个文件 - UIDevice + JEFkit.h
#import <UIKit/UIKit.h>
@interface UIDevice (JEFkit)
#pragma mark device type..
+(NSString *)deviceType;
+(BOOL)iPad;
+(BOOL)iPadMini;
+(BOOL)fourInchScreen;
+(BOOL)retinaDisplay;
+(Float32) pixelsPer10mm;
#pragma mark orientation..
+(UIInterfaceOrientation)orient;
+(BOOL)landscape;
#pragma mark specific feature checkers..
+(NSString *)deviceName;
+(BOOL)camera;
+(NSString *)systemVersion;
+(BOOL)doesSystemVersionMeetRequirement:(NSString *)minRequirement;// eg NSString *reqSysVer = @"4.0"
+(BOOL)iOs7;
@end
主要的,其中几个只是我难以记住的东西的包装纸,例如deviceIdiom的东西,无论如何它只是一个'帮助'类扩展.. UIDevice + JEFkit.m
#import "UIDevice+JEFkit.h"
#include <sys/types.h>
#include <sys/sysctl.h>
@implementation UIDevice (JEFkit)
#pragma mark device type..
+(NSString *)deviceType{
NSString *result = [[UIDevice currentDevice] model];
return result;
}
+(BOOL)iPad{
BOOL result = NO;
#if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 30200)
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
result = YES;
} else {
result = NO;
}
#endif
return result;
}
+(BOOL)iPadMini{
if (![UIDevice iPad]) {
return NO;
}
size_t size1;
sysctlbyname("hw.machine", NULL, &size1, NULL, 0);
char *machine1 = malloc(size1 + 1);
sysctlbyname("hw.machine", machine1, &size1, NULL, 0);
machine1[size1] = 0;
if (strcmp(machine1, "iPad1,1") == 0 || strcmp(machine1, "iPad2,1") == 0 || strcmp(machine1, "iPad2,2") == 0 || strcmp(machine1, "iPad2,3") == 0 || strcmp(machine1, "iPad2,4") == 0 ) {
/* iPad 1 or 2 */
// JLog(@"iPad 1 or 2");
free(machine1);
return NO;
}
if ([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {
if ([[UIScreen mainScreen] scale] < 2.0) {
free(machine1);
return YES; //all other non retina devices are eliminated
}
}else{
///does not respond to @selector(scale)
/// should not ever happen
free(machine1);
return NO;
}
//ok only retina ipads are left...
if (strcmp(machine1, "iPad4,4") == 0 || strcmp(machine1, "iPad4,5") == 0 ) {
/* 2nd gen minis w retina*/
free(machine1);
return YES;
}
free(machine1);
return NO;
}
+(BOOL)fourInchScreen{
if (![UIDevice iPad] && ([UIScreen mainScreen].bounds.size.height == 568 || [UIScreen mainScreen].bounds.size.width == 568)) return YES;
else return NO;
}
+(BOOL)retinaDisplay{
BOOL result = NO;
if ([[UIScreen mainScreen]respondsToSelector:@selector(scale)]){
if ([[UIScreen mainScreen] scale] == 2.0) result = YES;
}
return result;
}
+(Float32) pixelsPer10mm{
CGFloat dpi;
if ([UIDevice iPadMini]) {
dpi = (Float32)163.0;
} else if ([UIDevice iPad]) {
dpi = (Float32)132.0;
}else {dpi = (Float32)163.0;}
// JLog(@"pixels /10 mm %.3f", dpi/2.54);
return (Float32)(dpi / (Float32)2.54);
//return 64.173;
}
#pragma mark orientation..
+(UIInterfaceOrientation)orient{
return [[UIApplication sharedApplication] statusBarOrientation];
}
+(BOOL)landscape{
if (UIInterfaceOrientationIsLandscape([UIDevice orient])) {
return YES;
}
return NO;
}
#pragma mark specific feature checkers..
+(NSString *)deviceName{
return [[UIDevice currentDevice] name];
}
+(BOOL)camera{
BOOL result = [UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera];
return result;
}
+(NSString *)systemVersion{
NSString *result = [[UIDevice currentDevice] systemVersion];
return result;
}
+(BOOL)doesSystemVersionMeetRequirement:(NSString *)minRequirement{
// eg NSString *reqSysVer = @"4.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:minRequirement options:NSNumericSearch] != NSOrderedAscending)
{
return YES;
}else{
return NO;
}
}
+(BOOL)iOs7{
return (NSClassFromString(@"NSLayoutManager") != nil);
}
@end
所以要使用这个类别我只是将它添加到项目中(实际上它是我的'jefkit'静态库的一部分,但如果我只是添加这个,id进入我的.pch文件并添加
#import "UIDevice+JEFkit.h"
答案 6 :(得分:0)
我添加了对最长边缘的检查,因为边界值会因设备方向而发生变化。我使用最长边而不是设备方向,因为有时方向可以报告为未知。我在UIDevice扩展中有这个:
extension UIDevice {
func isLargerPhone() -> Bool {
let threePointFiveInchScreenHeight: CGFloat = 480
let dimensionToCheck = max(UIScreen.mainScreen().bounds.size.height, UIScreen.mainScreen().bounds.size.width)
let isLargerPhone = dimensionToCheck > threePointFiveInchScreenHeight && userInterfaceIdiom == .Phone
return isLargerPhone
}
func isSmallerPhone() -> Bool {
return !isLargerPhone() && userInterfaceIdiom == .Phone
}
}