码迷,mamicode.com
首页 > 其他好文 > 详细

高大上的宏定义#define

时间:2015-01-19 22:34:30      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

宏定义在很多语言里面都可以看到它的影子,C# ,C++,C等,尤其是系统API里面很多宏定义,看着很唬人。合理的利用宏定义,可以让自己的代码看着更规范,更方便,尤其在特定的某些地方。使用宏定义还可以让自己的代码看着高大上有木有!

本文改引自http://www.cnblogs.com/ios8/p/ios-hong.html

网上对IOS的宏定义比较多,我总结了一些最常用的宏,后续还会继续补上。

 

1.首次启动判断:

 

#define First_Launched @"firstLaunch"

2.ios7系统判断:

 

#define IsIOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0 ? YES : NO)

3.判断是否Retina屏、是否IPhone5、是否IPad

#define isRetina ([UIScreen instancesRespondToSelector:

@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), 

[[UIScreen mainScreen] currentMode].size) : NO)

 

#define iPhone5 ([UIScreen instancesRespondToSelector:

@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), 

[[UIScreen mainScreen] currentMode].size) : NO)

#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

 

 

4.加载图片宏:

 

#define LOADIMAGE(file,type) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:type]]

 

5.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] 

6.设置颜色RGB

#define COLOR(R, G, B, A) 

[UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A] 

 

7.去除"-(id)performSelector:(SEL)aSelector withObject:(id)object;"的警告

#define SuppressPerformSelectorLeakWarning(Stuff) \

do { \

_Pragma("clang diagnostic push") \

_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \

Stuff; \

_Pragma("clang diagnostic pop") \

} while (0)

8.圆周运动时角度与弧度的转换。

9.获取屏幕 宽度、高度

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
 
 
//----------------------系统----------------------------
 
// 是否iPad
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// 是否iPad
#define someThing (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)? ipad: iphone
 
//获取系统版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
 
//获取当前语言
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
 
//判断是否 Retina屏、设备是否%fhone 5、是否是iPad
#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
 
//判断设备的操做系统是不是ios7
#define IOS7 ([[[UIDevice currentDevice].systemVersion doubleValue] >= 7.0]
 
//判断当前设备是不是iphone5
#define kScreenIphone5    (([[UIScreen mainScreen] bounds].size.height)>=568)
 
//获取当前屏幕的高度
#define kMainScreenHeight ([UIScreen mainScreen].applicationFrame.size.height)
 
//获取当前屏幕的宽度
#define kMainScreenWidth  ([UIScreen mainScreen].applicationFrame.size.width)
 
 
 
//定义一个define函数
#define TT_RELEASE_CF_SAFELY(__REF) { if (nil != (__REF)) { CFRelease(__REF); __REF = nil; } }
 
//判断是真机还是模拟器
#if TARGET_OS_IPHONE
//iPhone Device
#endif
 
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
 
//检查系统版本
#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

高大上的宏定义#define

标签:

原文地址:http://www.cnblogs.com/lvchenhao/p/4234785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!