iOS開發(fā)常用宏定義,加快開發(fā)速度

系統(tǒng)高度

//屏幕的寬高
#define HitoScreenW [UIScreen mainScreen].bounds.size.width
#define HitoScreenH [UIScreen mainScreen].bounds.size.height
//屏幕大小
#define HitoScreenSize [UIScreen mainScreen].bounds
//比例寬和高(以6s為除數(shù))
#define HitoActureHeight(height)  roundf(height/375.0 * HitoScreenW)
#define HitoActureWidth(Width)  roundf(Width/667.0 * HitoScreenH)
//狀態(tài)欄的高度
#define HitoStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
//導航欄的高度
#define HitoNavBarHeight 44.0
//iphoneX-SafeArea的高度
#define HitoSafeAreaHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?34:0)
//分欄+iphoneX-SafeArea的高度
#define HitoTabBarHeight (49+HitoSafeAreaHeight)
//導航欄+狀態(tài)欄的高度
#define HitoTopHeight (HitoStatusBarHeight + HitoNavBarHeight)

快速查詢一段代碼的執(zhí)行時間

#define TOCK NSLog(@"Time:%f", -[[NSDate date] timeIntervalSinceNow]);

blcok 和self 引起的循環(huán)引用警告??的處理問題

#define Custom_Weak(weakSelf)  __weak __typeof(&*self)weakSelf = self;

代碼中加入NSLog的暴力調試方法是很頻繁的未巫,但是在release的時候要刪除這些調試代碼,那工作量是煩躁,這樣的情況下兰英,試用宏就會顯得非常的方便

//非常實用的打印,包括打印的類名——方法名-行數(shù)-打印的內容
#ifdef DEBUG
#define LRLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define LRLog(...)
#endif

獲得主 window

#define Window [[UIApplication sharedApplication].delegate window]

自動退出的提示框供鸠,需要和獲得的主 window 結合使用畦贸,同時導入第三方庫 Toast

#define Toast(str)  CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle]; \
[Window  makeToast:str duration:0.6 position:CSToastPositionCenter style:style];\
Window.userInteractionEnabled = NO; \
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{\
Window.userInteractionEnabled = YES;\
});\

iPad Air {{0, 0}, {768, 1024}}

iPhone4s {{0, 0}, {320, 480}} 960640
iPhone5 5s {{0, 0}, {320, 568}} 1136
640
iPhone6 6s {{0, 0}, {375, 667}} 1334750
iPhone6Plus 6sPlus {{0, 0}, {414, 736}} 1920
1080
Apple Watch 1.65inches(英寸) 320*640

獲取手機的 UUID以及屏幕的尺寸

#define UUID [[[UIDevice currentDevice] identifierForVendor] UUIDString]

#define APP_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define APP_HEIGHT ([UIScreen mainScreen].bounds.size.height)

#define TabBarH        49.0f
#define StatusBarH     20.0f
#define NavigationBarH 44.0f

// 根據(jù) iPhone6 6s 的尺寸為例
#define WIDTH  APP_WIDTH/375
#define HEIGHT APP_HEIGHT/667

獲取View的屬性

#define Get_View_Width(Width)  view.frame.size.width
#define Get_View_Height(Height) view.frame.size.height
#define Get_View_X(X)      view.frame.origin.x
#define Get_View_Y(Y)      view.frame.origin.y

分辨設備類型

//判斷是否 Retina屏、設備是否iPhone 5楞捂、是否是iPad
#define Is_Retina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)

/** 判斷是否為iPhone */
#define Is_iPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

/** 判斷是否是iPad */
#define Is_iPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

/** 判斷是否為iPod */
#define Is_iPod ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])

/** 設備是否為iPhone 4/4S 分辨率320x480薄坏,像素640x960,@2x */
#define iPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)

/** 設備是否為iPhone 5C/5/5S 分辨率320x568,像素640x1136,@2x */
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

/** 設備是否為iPhone 6 分辨率375x667述呐,像素750x1334试浙,@2x */
#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)

/** 設備是否為iPhone 6 Plus 分辨率414x736,像素1242x2208物舒,@3x */
#define iPhone6P ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

設置View的圓角和邊框

#define View_Border_Radius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

由角度轉換弧度/由弧度轉換角度

#define DegreesToRadian(x) (M_PI * (x) / 180.0)

#define RadianToDegrees(radian) (radian*180.0)/(M_PI)

獲取圖片

獲取圖片資源
#define GetImage(imageName) [UIImage imageNamed:imageName]
讀取本地圖片
#define Load_Image(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]

獲取文件路徑

//獲取temp
#define Path_Temp NSTemporaryDirectory()
//獲取沙盒 Document
#define Path_Document [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//獲取沙盒 Cache
#define Path_Cache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

GCD 的宏定義(需要自己加上^{})

//GCD - 一次性執(zhí)行
#define DISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
//GCD - 在Main線程上運行
#define DISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
//GCD - 開啟異步線程
#define DISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);

設置顏色

//設置隨機顏色
#define Random_Color [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
設置顏色和透明度
#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)]
// 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]

//帶有RGBA的顏色設置
#define Color_Alpha(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]

#define Color_Clear [UIColor clearColor]

#define COLOR_BLUE_             UIColorFromRGB(0x41CEF2)
#define COLOR_GRAY_             UIColorFromRGB(0xababab) //171
#define COLOR_333               UIColorFromRGB(0x333333) //51
#define COLOR_666               UIColorFromRGB(0x666666) //102
#define COLOR_888               UIColorFromRGB(0x888888) //136
#define COLOR_999               UIColorFromRGB(0x999999) //153
#define COLOR_PLACEHOLD_        UIColorFromRGB(0xc5c5c5) //197
#define COLOR_RED_              UIColorFromRGB(0xff5400) //紅色
#define COLOR_GREEN_            UIColorFromRGB(0x31d8ab)//綠色
#define COLOR_YELLOW_           UIColorFromRGB(0xffa200)//黃色
#define COLOR_SEPARATE_LINE     UIColorFromRGB(0xC8C8C8)//200
#define COLOR_LIGHTGRAY         COLOR(200, 200, 200, 0.4)//淡灰色

判斷操作系統(tǒng)版本

#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

/** 獲取系統(tǒng)版本 */
#define iOS_VERSION ([[[UIDevice currentDevice] systemVersion] floatValue])
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])

/** 是否為iOS6 */
#define iOS6 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) ? YES : NO)

/** 是否為iOS7 */
#define iOS7 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) ? YES : NO)

/** 是否為iOS8 */
#define iOS8 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) ? YES : NO)

/** 是否為iOS9 */
#define iOS9 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) ? YES : NO)

NSUserDefaults 存/獲得/刪對象

//存儲對象

#define UserDefaultSetObjectForKey(__VALUE__,__KEY__) \
{\
[[NSUserDefaults standardUserDefaults] setObject:__VALUE__ forKey:__KEY__];\
[[NSUserDefaults standardUserDefaults] synchronize];\
}
 //獲得存儲的對象
#define UserDefaultObjectForKey(__KEY__)  [[NSUserDefaults standardUserDefaults] objectForKey:__KEY__]

//     刪除對象
#define UserDefaultRemoveObjectForKey(__KEY__) \
{\
[[NSUserDefaults standardUserDefaults] removeObjectForKey:__KEY__];\
[[NSUserDefaults standardUserDefaults] synchronize];\
}

宏定義一個彈窗方法,括號里面是方法的參數(shù)

#define ShowAlert(message)    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"溫馨提示" message:message delegate:self cancelButtonTitle:@"cancel" otherButtonTitles: @"OK"];[alert show];

與后臺連調的宏定義

//定義一個API
#define API_URL                @"http://www.google.com"
//login the API 登陸API
#define API_Login              [APIURL stringByAppendingString:@"Login"]

//根據(jù)不同的系統(tǒng),設置不同的動畫

#define YES_Or_NO    floorf([[UIDevice currentDevice].systemVersion floatValue]) >8.0 ? YES : NO

借鑒別人的,加上自己的一些經驗闻牡,希望對大家有用,也希望大家補充

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末绳矩,一起剝皮案震驚了整個濱河市罩润,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌翼馆,老刑警劉巖割以,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異应媚,居然都是意外死亡拳球,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門珍特,熙熙樓的掌柜王于貴愁眉苦臉地迎上來祝峻,“玉大人,你說我怎么就攤上這事±痴遥” “怎么了酬姆?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長奥溺。 經常有香客問我辞色,道長,這世上最難降的妖魔是什么浮定? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任相满,我火速辦了婚禮,結果婚禮上桦卒,老公的妹妹穿的比我還像新娘立美。我一直安慰自己,他們只是感情好方灾,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布建蹄。 她就那樣靜靜地躺著,像睡著了一般裕偿。 火紅的嫁衣襯著肌膚如雪洞慎。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天嘿棘,我揣著相機與錄音劲腿,去河邊找鬼。 笑死鸟妙,一個胖子當著我的面吹牛谆棱,可吹牛的內容都是我干的。 我是一名探鬼主播圆仔,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼垃瞧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了坪郭?” 一聲冷哼從身側響起个从,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎歪沃,沒想到半個月后嗦锐,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡沪曙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年奕污,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片液走。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡碳默,死狀恐怖贾陷,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情嘱根,我是刑警寧澤髓废,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站该抒,受9級特大地震影響慌洪,放射性物質發(fā)生泄漏。R本人自食惡果不足惜凑保,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一冈爹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧欧引,春花似錦频伤、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽惠爽。三九已至癌蓖,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間婚肆,已是汗流浹背租副。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留较性,地道東北人用僧。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像赞咙,于是被迫代替她去往敵國和親责循。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內容