IOS 常用的宏定义(#define)

开发中经常用到的常量定义(随时更行):

与UIView相关

//获取View的frame属性
#define GetViewWidth(view)  view.frame.size.width
#define GetViewHeight(view) view.frame.size.height
#define GetViewX(view)      view.frame.origin.x
#define GetViewY(view)      view.frame.origin.y

与屏幕相关

//屏幕尺寸(宽高)常量
#define GetScreenWidth      [[UIScreen mainScreen] bounds].size.width
#define GetScreenHeight     [[UIScreen mainScreen] bounds].size.height

与系统版本相关

//获取系统版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define IOS6 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0 && [[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
#define IOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0 && [[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
#define IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 && [[[UIDevice currentDevice] systemVersion] floatValue] < 9.0)
#define IOS9 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0 && [[[UIDevice currentDevice] systemVersion] floatValue] < 10.0)

与UIColor相关

//取得对应的UIcolor
#define UIColorFromRGB(R, G, B) [UIColor colorWithRed:((R) / 255.0f) green:((G) / 255.0f) blue:((B) / 255.0f) alpha:1.0f]
#define UIColorFromRGBA(R, G, B, A) [UIColor colorWithRed:((R) / 255.0f) green:((G) / 255.0f) blue:((B) / 255.0f) alpha:A]
#define UIColorFromHex(Hex) [UIColor colorWithRed:((Hex & 0xFF0000) >> 16)/255.0f green:((Hex & 0xFF00) >> 8)/255.0f blue:((Hex & 0xFF))/255.0f alpha:1.0f]
#define UIColorFromHexA(Hex, A) [UIColor colorWithRed:((Hex & 0xFF0000) >> 16)/255.0f green:((Hex & 0xFF00) >> 8)/255.0f blue:((Hex & 0xFF))/255.0f alpha:A]

与GCD相关

//只运行一次的GCD
#define DISPATCH_ONCE_BLOCK(Block) static dispatch_once_t onceToken; dispatch_once(&onceToken, Block);

//在Main线程上运行的GCD
#define DISPATCH_ON_MAIN_THREAD(Block) dispatch_async(dispatch_get_main_queue(), Block);

//在Global Queue上运行的GCD
#define DISPATCH_ON_GLOBAL_QUEUE_HIGH(Block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), Block);
#define DISPATCH_ON_GLOBAL_QUEUE_DEFAULT(Block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), Block);
#define DISPATCH_ON_GLOBAL_QUEUE_LOW(Block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), Block);
#define DISPATCH_ON_GLOBAL_QUEUE_BACKGROUND(Block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), Block);

与弧度、角度相关

//用角度生成弧度
#define RadiansFromDegrees(Degress) (M_PI * (Degress) / 180.0)

与UIImage相关

//获取图片资源
#define GetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
原文地址:https://www.cnblogs.com/nmzd/p/4532965.html