前言
在pch
文件中定义常用的宏可以极大的提高开发效率,改善代码臃肿,一下是我开发常用的一些宏定义,供大家参考。
1.获取屏幕宽度
//不考虑横屏模式
# define SCREEN_WITCH [UIScreen mainScreen].bounds.size.width
# define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
//支持横屏模式下
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 当前Xcode支持iOS8及以上
#define SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)
#define SCREENH_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height)
#define SCREEN_SIZE ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?CGSizeMake([UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale,[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale):[UIScreen mainScreen].bounds.size)
#else
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
#endif
2.获取通知中心
#define ZHLNotificationCenter [NSNotificationCenter defaultCenter]
3.生成颜色
//随机颜色
#define ZHLRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
//RGB颜色设置
#define ZHLRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define ZHLRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
//透明颜色
#define ZHLClearColor [UIColor clearColor]
4.打印信息语句
只在调试的时候出现
#ifdef DEBUG
#define ZHLLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define ZHLLog(...)
#endif
5.块中强弱引用
#define Weakify(type) __weak typeof(type) weak##type = type;
#define Strongfy(type) __strong typeof(type) type = weak##type;
6.弧度与角度转换
#define ZHLDegreesToRadian(x) (M_PI * (x) / 180.0)
#define ZHLRadianToDegrees(radian) (radian*180.0)/(M_PI)
7.判断设备和版本
//判断是否为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 5SE
#define iPhone5SE [[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
//获取系统版本
#define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//判断 iOS 8 或更高的系统版本
#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))
8.宏与const的使用
- 宏的用法: 一般字符串抽成宏,代码抽成宏使用。
- const用法:一般常用的字符串定义成const(对于常量字符串苹果推荐我们使用const)。
宏与const区别: - 1.编译时刻不同,宏属于预编译 ,const属于编译时刻
- 2.宏能定义代码,const不能,多个宏对于编译会相对时间较长,影响开发效率,调试过慢,const只会编译一次,缩短编译时间。
- 3.宏不会检查错误,const会检查错误。
通过以上对比,我们以后在开发中如果定义一个常量字符串就用const,定义代码就用宏:
//.h文件
UIKIT_EXTERN NSString * const APP_TOKEN;
//.m文件
NSString * const APP_TOKEN = @"flksnfoewoiruue3u2e32423";
以上是自己实际开发中使用的宏定义和网上收集的一些,文章持续更新。欢迎交流和补充。