Object-C開發常用宏定義


Object-C開發中宏會將經常用到的系統常量進行封裝,方便使用:

 

// 1、獲取通知中心
#define EYNotificationCenter(name, object, userInfo) [[NSNotificationCenter defaultCenter] postNotificationName:name object:object userInfo:userInfo]



// 2、設置 view 圓角和邊框
#define EYViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]



// 3、由角度轉換弧度 由弧度轉換角度
#define EYDegreesToRadian(x) (M_PI * (x) / 180.0)
#define EYRadianToDegrees(radian) (radian*180.0)/(M_PI)



// 4、獲取圖片資源
#define EYGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]

 


// 5、獲取當前語言
#define EYCurrentLanguage ([NSLocale preferredLanguages].firstObject)

 

 

// 6、判斷當前項目的環境
#if __has_feature(objc_arc)
// 編譯器是ARC環境
#else
// 編譯器是MRC環境
// [p release];
#endif

 



// 7、判斷當前的iPhone設備/系統版本
//判斷是否為iPhone
#define IS_IPHONE ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])

//判斷是否為iPad
#define EYiPAD ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"])

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

// 判斷是否為 iPhone SE
#define EYiPhoneSE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f

// 判斷是否為iPhone 7
#define EYiPhone7 [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f

// 判斷是否為iPhone 7Plus
#define EYiPhone7Plus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f

 



// 8、獲取系統版本
#define EYSystemVersion [[UIDevice currentDevice] systemVersion]

//判斷 iOS 8 或更高的系統版本
#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))

 

 

// 9、判斷是真機還是模擬器
#if TARGET_OS_IPHONE
//iPhone Device
#endif

#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif

 


// 10、沙盒目錄文件
//獲取temp
#define EYPathTemp NSTemporaryDirectory()

//獲取沙盒 Document
#define EYPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]

//獲取沙盒 Cache
#define EYPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

 


// 11.字符串是否為空
#define EYStringIsEmpty(string) ([string isKindOfClass:[NSNull class]] || string.length == 0 ? YES : NO)

 

 

// 12.數組是否為空
#define EYArrayIsEmpty(array) ([array isKindOfClass:[NSNull class]] || array.count == 0 ? YES : NO)

 


// 13.字典是否為空
#define EYDictionaryIsEmpty(dictionary) ([dictionary isKindOfClass:[NSNull class]] || dictionary.allKeys == 0 ? YES : NO)

 


// 14.自定義Log
#ifdef DEBUG
#define EYLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define EYLog(...)

#endif

 

 

// 15.顏色
#define EYRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

// RGB顏色 red green blue alpha 為系統關鍵字,不能使用
#define EYRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)/1.0]

// 隨機色
#define EYRandomColor EYRGBAColor(arc4random_uniform(256), arc4random_uniform(256), arc4random_uniform(256), 1.0)

// clear背景顏色
#define EYClearColor [UIColor clearColor]



// 16.屏幕的寬度

不要用self.view.bounds.size.width 因為view可能是Xib創建的,導致取出來的寬度不准確,需要取出當前使用設備的屏幕
#define EYScreenWidth [[UIScreen mainScreen] bounds].size.width

#define EYScreenHeight [[UIScreen mainScreen] bounds].size.height

 

// 17.子線程中,使用宏將更新UI的任務派發到主隊列
#define dispatch_main_sync_safe(block) \
if ([NSThread isMainThread]) { \
block(); \
} else { \
dispatch_sync(dispatch_get_main_queue(), block); \
}

#define dispatch_async_main(block) dispatch_async(dispatch_get_main_queue(), block)

 

 

 

更多內容--> 博客導航 每周一篇喲!!!

 

 

有任何關於iOS開發的問題!歡迎下方留言!!!或者郵件lieryangios@126.com 雖然我不一定能夠解答出來,但是我會請教iOS開發高手!!!解答您的問題!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM