iOS開(kāi)發(fā)_常用的宏定義

在這里給大家分享一些常用的宏定義,喜歡的小伙伴可以直接在項(xiàng)目中使用。

目錄

1.獲取屏幕寬度與高度
2.獲取通知中心
3.設(shè)置隨機(jī)顏色
4.設(shè)置RGB顏色/設(shè)置RGBA顏色
5.自定義高效率的NSLog
6.弱引用/強(qiáng)引用
7.設(shè)置view圓角和邊框
8.由角度轉(zhuǎn)換弧度/由弧度轉(zhuǎn)換角度
9.獲取view的frame/圖片資源
10.獲取當(dāng)前語(yǔ)言
11.使用ARC和MRC
12.判斷當(dāng)前的iPhone設(shè)備/系統(tǒng)版本
13.沙盒目錄文件
14.GCD的宏定義

常用宏定義

1.獲取屏幕寬度與高度

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

2.獲取通知中心

#define SHNotificationCenter [NSNotificationCenter defaultCenter]

3.設(shè)置隨機(jī)顏色

#define SHRandomColor [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1.0]

4.設(shè)置RGB顏色/設(shè)置RGBA顏色

#define SHRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define SHRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]

5.自定義高效率的NSLog

#ifdef DEBUG
#define SHLog(...) NSLog(@"%s 第%d行 %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define SHLog(...)
#endif

6.弱引用/強(qiáng)引用

#define SHWeakSelf(type) __weak typeof(type) weak##type = type;
#define SHStrongSelf(type) __strong typeof(type) type = weak##type;

7.設(shè)置view圓角和邊框

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

8.由角度轉(zhuǎn)換弧度/由弧度轉(zhuǎn)換角度

#define SHDegreesToRadian(x) (M_PI * (x) / 180.0)
#define SHRadianToDegrees(randian) (randian * 180.0) / (M_PI)

9.獲取view的frame/圖片資源

// 獲取view的frame
#define kGetViewWidth(view) view.frame.size.width
#define kGetViewHeight(view) view.frame.size.height
#define kGetViewX(view) view.frame.origin.x
#define kGetViewY(view) view.frame.origin.y
// 獲取圖片資源
#define kGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]

10.獲取當(dāng)前語(yǔ)言

#define SHCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

11.使用ARC和MRC

#if __has_feature(objc_arc)
// ARC
#else
// MRC
#endif

12.判斷當(dāng)前的iPhone設(shè)備/系統(tǒng)版本

// 判斷是否為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 5/SE
#define iPhoneSE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f
// 判斷是否為iPhone 6/6s
#define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f
// 判斷是否為iPhone 6Plus/6sPlus
#define iPhone6Plus_6sPlus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f
// 獲取系統(tǒng)版本
#define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
// 判斷 iOS 8 或更高的系統(tǒng)版本
#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))

13.沙盒目錄文件

// 獲取temp
#define kPathTemp NSTemporaryDirectory()
// 獲取沙盒 Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
// 獲取沙盒 Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

14.GCD的宏定義

// GCD - 一次性執(zhí)行
#define kDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
// GCD - 在Main線程上運(yùn)行
#define kDISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
// GCD - 開(kāi)啟異步線程
#define kDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);

小結(jié)

文件已經(jīng)上傳至git:
--> 傳送門:https://github.com/272095249/SHMacroDefinition.git

有問(wèn)題歡迎指正以及相互探討 —— CoderSun

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 大家都是知道開(kāi)發(fā)中使用宏不僅方便,而且可以提高開(kāi)發(fā)效率, 代碼清晰易懂。下面我總結(jié)了我在做iOS開(kāi)發(fā)時(shí)的一些常用宏...
    AbnerZhang閱讀 381評(píng)論 0 2
  • 一個(gè)好的iOS開(kāi)發(fā)工程師必須學(xué)會(huì)使用宏定義,不僅可以提高開(kāi)發(fā)效率,而且高端、大氣、上檔次。下面是我總結(jié)的一些常用的...
    my_楊哥閱讀 1,125評(píng)論 6 20
  • Objective-C常用宏/*! 字體 */ /*! 顏色宏定義 */ /*! 弱引用宏 */ /*! 輸出顯示...
    葬己閱讀 747評(píng)論 0 0
  • 原文鏈接:http://www.lxweimin.com/p/213b3b96cafe 在工作中, 很多小伙伴都會(huì)在...
    為了你而活閱讀 442評(píng)論 0 3
  • 來(lái)源于CocoaChina 在工作中, 很多小伙伴都會(huì)在PCH文件定義一些常用的宏,但是又怕寫這些簡(jiǎn)單的宏浪費(fèi)時(shí)間...
    iOS學(xué)末閱讀 832評(píng)論 3 7