[英]iPhone 3.5 Inch screen - 4 inch screen:
I know you can choose ipad or iphone by using an if statement with UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
. 我知道你可以通过使用带有
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
的if语句来选择ipad或iphone。 But I was wondering if you could do the same for iPhone 3.5 inch screen to iPhone 4 inch screen. 但我想知道你是否可以为iPhone 3.5英寸屏幕和iPhone 4英寸屏幕做同样的事情。 I have a game which was originally designed for the iPhone 4 inch screen, but auto layout has been used to fix the errors between the two devices, the one thing I need to change is that when
我有一款最初设计用于iPhone 4英寸屏幕的游戏,但是自动布局已用于修复两个设备之间的错误,我需要改变的一件事就是当
if (ball.center.y > 600) {
RandomPosition = arc4random() %248;
RandomPosition = RandomPosition + 36;
ball.center = CGPointMake(RandomPosition, -22);
}
Because it was originally designed on the 4 inch screen, the game is set up to reset the ball to the top of the screen when it is > 600 pixels, which is just below the iphone 4 inch screen. 因为它最初是在4英寸屏幕上设计的,所以游戏设置为当它大于600像素时将球重置到屏幕顶部,这正好在iphone 4英寸屏幕下方。 It still functions properly, there is just a bit of a delay between the bottom of the 3.5 inch screen and the resetting of the position.
它仍能正常工作,3.5英寸屏幕底部和位置重置之间只有一点延迟。 Is there anyway I could set up an if statement with
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom(iphone4???)
to set up a new ball.center.y > 300 or something? 无论如何我可以用
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom(iphone4???)
设置一个if语句来设置一个新的ball.center.y> 300或者什么?
You can try following code. 您可以尝试以下代码。 Add this code in Constant.h
在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)
Now to check the device size add the following code in your Views 现在要检查设备大小,请在视图中添加以下代码
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
}
}
you can check for 3.5 inch and 4 inch by suing below statement 您可以通过以下声明检查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");
}
}
Based on the other answers, if you plan to do this check a lot, it might make sense to create a macro for it: 根据其他答案,如果您计划进行大量检查,可能需要为它创建一个宏:
#define isLargerPhone() (([UIScreen mainScreen].bounds.size.height > 480) && [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
Put this in a MyMacros.h
(or similar) which you can import into your Prefix.pch
file, then whenever you need to perform the check you can simply do: 将它放在
MyMacros.h
(或类似的)中,您可以将其导入到Prefix.pch
文件中,然后无论何时需要执行检查,您都可以执行以下操作:
if ( isLargePhone()){
// do whatever...
}
You may use below method to check if iphone is iPhone 5 ie of 4inch 您可以使用以下方法检查iPhone是否是iPhone 5即4英寸
- (BOOL)hasFourInchDisplay {
return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0);
}
Use this method to know device type and model like iPhone5/4S/4 or other 使用此方法可以了解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
}
}
}
I hope this will help 我希望这个能帮上忙
ok i'm here it is in a category, sorry to put as answer but it allows me better formatting... 好吧我在这里它属于一个类别,很抱歉作为答案但它允许我更好的格式化...
first file - UIDevice+JEFkit.h 第一个文件 - 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
and the main, several of these are just wrappers for stuff i struggle to remember, eg the deviceIdiom stuff, anyway its just a little 'helpers' class extension.. UIDevice+JEFkit.m 主要的,其中几个只是我难以记住的东西的包装,例如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
so to use this category i just add it to project (actually its part of my 'jefkit' static library, but if i was to add just this, id go into my .pch file and add 所以要使用这个类别我只是将它添加到项目(实际上它是我的'jefkit'静态库的一部分,但如果我只是添加这个,id进入我的.pch文件并添加
#import "UIDevice+JEFkit.h"
I added a check for the longest edge since the value of the bounds changes for device orientation. 我添加了一个最长边的检查,因为边界的值会因设备方向而发生变化。 I used the longest edge instead of device orientation as sometimes orientation can be reported as Unknown.
我使用最长边而不是设备方向,因为有时方向可以报告为未知。 I have this in a UIDevice extension:
我在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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.