IOS快速開發之常量定義
在IOS開發中,有一些方法常常需要用的,但是有很長的方法名,這造成了代碼長,寫起來累,我們可以通過宏定義瞭解決這些問題 比如說在代碼布局的時候會遇上這樣的問題,我們要擷取上面一個的Y軸座標,有兩種方法 通過座標加上高度來計算 xx.frame.origin.y+xx.frame.size.height還有一個略微簡便的方法 CGRectGetMaxY(xx.frame)都挺麻煩的,這時候就需要祭出宏定義來幫忙 #define MaxY(v) CGRectGetMaxY((v).frame) //縱座標加上控制項的高度通過這樣的定義,下次我們擷取Y座標的時候只要輕輕的寫上MaxY(xx)即可,是不是簡單很多呢? 類似的還有載入圖片的方法 #define IMAGENAMED(NAME) [UIImage imageNamed:NAME]這樣就非常的方便, 下面是我整理的一個類 #ifndef Medical_Wisdom_UConstants_h#define Medical_Wisdom_UConstants_h /******************************************************/ /**** debug log **/ //NSLog輸出資訊 #ifdef DEBUG #define DLog( s, ... ) NSLog( @"< %@:(%d) > %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] ) #else #define DLog( s, ... ) #endif /*** DEBUG RELEASE */ #if DEBUG #define MCRelease(x) #else #define MCRelease(x) #endif /***** release *****/#define NILRelease [x release], x = nil #pragma mark - Frame(宏 x,y,width,height) #define MainScreenScale [[UIScreen mainScreen]scale] //螢幕的解析度 當結果為1時,顯示的是普通螢幕,結果為2時,顯示的是Retian螢幕// App Frame Height&Width#define Application_Frame [[UIScreen mainScreen] applicationFrame] //除去訊號區的螢幕的frame#define APP_Frame_Height [[UIScreen mainScreen] applicationFrame].size.height //應用程式的螢幕高度#define App_Frame_Width [[UIScreen mainScreen] applicationFrame].size.width //應用程式的螢幕寬度/*** MainScreen Height Width */ #define Main_Screen_Width [[UIScreen mainScreen] bounds].size.width //主畫面的寬度#define Main_Screen_Height [[UIScreen mainScreen] bounds].size.height //主畫面的高度#define Main_Screen_Height_without_top [[UIScreen mainScreen] bounds].size.height-64 //主畫面的高度 // View 座標(x,y)和寬高(width,height)#define X(v) (v).frame.origin.x#define Y(v) (v).frame.origin.y#define WIDTH(v) (v).frame.size.width#define HEIGHT(v) (v).frame.size.height #define MinX(v) CGRectGetMinX((v).frame) // 獲得控制項螢幕的x座標#define MinY(v) CGRectGetMinY((v).frame) // 獲得控制項螢幕的Y座標 #define MidX(v) CGRectGetMidX((v).frame) //橫座標加上到控制項中點座標#define MidY(v) CGRectGetMidY((v).frame) //縱座標加上到控制項中點座標 #define MaxX(v) CGRectGetMaxX((v).frame) //橫座標加上控制項的寬度#define MaxY(v) CGRectGetMaxY((v).frame) //縱座標加上控制項的高度 #define CONTRLOS_FRAME(x,y,width,height) CGRectMake(x,y,width,height) // 系統控制項的預設高度#define kStatusBarHeight (20.f)#define kTopBarHeight (44.f)#define kBottomBarHeight (49.f) #define kCellDefaultHeight (44.f) #define KstatusBarAndNavigation (64.0)// 當控制項為全屏時的橫縱左邊#define kFrameX (0.0)#define kFrameY (0.0) #define kPhoneWithStatusNoPhone5Height (480.0)#define kPhoneNoWithStatusNoPhone5Height (460.0)#define kPhoneWithStatusPhone5Height (568.0)#define kPhoneNoWithStatusPhone5Height (548.0) #define kPadFrameWidth (768.0)#define kPadWithStatusHeight (1024.0)#define kPadNoWithStatusHeight (1004.0) //中英狀態下鍵盤的高度#define kEnglishKeyboardHeight (216.f)#define kChineseKeyboardHeight (252.f) #pragma mark - Funtion Method (宏 方法)//PNG JPG 圖片路徑#define PNGPATH(NAME) [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:NAME] ofType:@"png"]#define JPGPATH(NAME) [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:NAME] ofType:@"jpg"]#define PATH(NAME,EXT) [[NSBundle mainBundle] pathForResource:(NAME) ofType:(EXT)] //載入圖片#define PNGIMAGE(NAME) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(NAME) ofType:@"png"]]#define JPGIMAGE(NAME) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(NAME) ofType:@"jpg"]]#define IMAGE(NAME,EXT) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(NAME) ofType:(EXT)]]#define IMAGENAMED(NAME) [UIImage imageNamed:NAME] //字型大小(常規/粗體)#define BOLDSYSTEMFONT(FONTSIZE) [UIFont boldSystemFontOfSize:FONTSIZE]#define SYSTEMFONT(FONTSIZE) [UIFont systemFontOfSize:FONTSIZE]#define FONT(NAME,FONTSIZE) [UIFont fontWithName:(NAME) size:(FONTSIZE)] //顏色(RGB)#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)] #define BACKGROUND_COLOR [UIColor colorWithRed:22/255.0f green:29/255.0f blue:38/255.0f alpha:1]//目前的版本#define FSystenVersion ([[[UIDevice currentDevice] systemVersion] floatValue])#define DSystenVersion ([[[UIDevice currentDevice] systemVersion] doubleValue])#define SSystemVersion ([[UIDevice currentDevice] systemVersion]) //當前語言#define CURRENTLANGUAGE ([[NSLocale preferredLanguages] objectAtIndex:0]) //是否Retina屏#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) :NO)//是否iPhone5#define ISIPHONE [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone#define ISIPHONE5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)#define ISIPHONE6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)#define ISIPHONE6P ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO) //是否是iPad#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define YELLO_APP [UIColor colorWithRed:255/255 green:148.0/255 blue:28.0/255 alpha:1] // UIView - viewWithTag 通過tag值獲得子視圖#define VIEWWITHTAG(_OBJECT,_TAG) [_OBJECT viewWithTag : _TAG] //應用程式的名字#define AppDisplayName [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"] //RGB色彩轉換(16進位->10進位)#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] //判斷裝置室真機還是模擬器#if TARGET_OS_IPHONE/** iPhone Device */#endif #if TARGET_IPHONE_SIMULATOR/** iPhone Simulator */#endif #endif這個在開發的時候可以略微的提高效率,OC的文法確實有點煩啊,趕快學swift吧,^_^