目录
1、PCH
1、PCH
用于
存放常用的
宏定义
头文件
常量使用const而不是宏:
1.宏过多会影响开发效率(宏是在预编译阶段处理的),const是在编译阶段处理的且只会被编译一次。
2.宏只是简单的替换,不会检查错误,而const会检查错误。
- 配置
- 常用
#pragma make 调试、线上
#ifdef DEBUG
// 高效打印(release模式下不会打印)
#define YTLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define YTLog(...)
#endif
#pragma make 保存
// NSUserDefaults
#define YTUserDefaults [NSUserDefaults standardUserDefaults]
// NSUserDefaults获取
#define YTUserDefaultsObject(KEY)\
[[NSUserDefaults standardUserDefaults] objectForKey:KEY]
// NSUserDefaults保存
#define YTUserDefaultsSave(OBJECT,KEY) \
[[NSUserDefaults standardUserDefaults] setObject:OBJECT forKey:KEY];\
[[NSUserDefaults standardUserDefaults] synchronize];
#pragma make 通知NotificationCenter
#define YTNotificationCenter [NSNotificationCenter defaultCenter]
#pragma make 弱强引用
#define YTWeakSelf(type) __weak typeof(type) weak##type = type;
#define YTStrongSelf(type) __strong typeof(type) type = weak##type;
#pragma make device类型
#define YT_IS_IPHONE ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
#define YT_IPHONE5S (UI_IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0)
#define YT_IPHONE6 (UI_IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0)
#define YT_IPHONE6PLUS (UI_IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0 || [[UIScreen mainScreen] bounds].size.width == 736.0)
#pragma make 图片
// 公用默认图片
#define YT_HeaderImg @"head"
// 获取本地图片
#define YTGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
#pragma make UI
// 需要横屏或者竖屏,获取屏幕宽度与高度
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
// 屏幕宽
#define YTSCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)
// 屏幕高
#define YTSCREENH_HEIGHT ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.height)
// 屏幕Size
#define YTSCREEN_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 YTSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define YTSCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
#define YTSCREEN_SIZE [UIScreen mainScreen].bounds.size
#endif
// keyWindow
#define kWindow [UIApplication sharedApplication].keyWindow
// 设置圆角
#define YTViewRadius(View, Radius)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];
// 设置边框
#define YTViewBorder(View, Width, Color)\
\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]];
// 设置圆角,边框
#define YTViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]];
// 16进制color
#define YTColorFromRGB(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]
// random color
#define YTRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
// rgb
#define YTRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
// rgba
#define YTRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
// clearColor
#define YTClearColor [UIColor clearColor]
// 角度转弧度
#define LRDegreesToRadian(x) (M_PI * (x) / 180.0)
// 弧度转角度
#define LRRadianToDegrees(radian) (radian*180.0)/(M_PI)
// 加载状态栏网络菊花
#define kShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
// 收起加载状态栏网络菊花
#define HideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO
// 设置加载状态栏网络菊花
#define NetworkActivityIndicatorVisible(x) [UIApplication sharedApplication].networkActivityIndicatorVisible = x
// 添加背景View(Window上的)
#define kBackView for (UIView *item in kWindow.subviews) { \
if(item.tag == 1100) \
{ \
[item removeFromSuperview]; \
UIView * aView = [[UIView alloc] init]; \
aView.frame = [UIScreen mainScreen].bounds; \
aView.tag = 1100; \
aView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3]; \
[kWindow addSubview:aView]; \
} \
} \
// 移除window背景View
#define kRemoveBackView for (UIView *item in kWindow.subviews) { \
if(item.tag == 1100) \
{ \
[UIView animateWithDuration:0.4 animations:^{ \
item.alpha = 0.0; \
} completion:^(BOOL finished) { \
[item removeFromSuperview]; \
}]; \
} \
} \
// 添加背景View和转圈
#define kShowHUDAndActivity kBackView;[MBProgressHUD showHUDAddedTo:kWindow animated:YES];kShowNetworkActivityIndicator()
// 移除背景View和转圈
#define kHiddenHUDAndAvtivity kRemoveBackView;kHiddenHUD;HideNetworkActivityIndicator()
// 当地语言
#define LRCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
// 判断是否为iPhone (2方式)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])
// 判断是否为iPad (2方式)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPAD ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"])
// 判断是否为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
// ios系统版本
#define IOS_SYSTEM_STRING [[UIDevice currentDevice] systemVersion]
// 判断 iOS 8 或更高的系统版本
#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))
// 判断真机还是模拟器
#if (TARGET_IPHONE_SIMULATOR)
// 模拟器
#else
// 真机
#endif
// ARC和MRC
#if __has_feature(objc_arc)
// ARC
#else
// MRC
#endif
// 沙盒根路径
#define YTHomePath NSHomeDirectory()
// Documents路径
#define YTDocumentPath [NSString stringWithFormat:@"%@/Documents",NSHomeDirectory()]
// Tmp路径
#define YTTmpPath NSTemporaryDirectory()
// Caches路径
#define YTCachePath [NSString stringWithFormat:@"%@/Library/Caches",NSHomeDirectory()]
//
#define YTFilePath(path) [NSString stringWithFormat:@"%@/%@",YTDocumentPath,path]
// GCD
// 运行期间仅执行一次
#define YT_DISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
// 主线程中执行
#define YT_DISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
// 全局(异步)线程中执行
#define YT_DISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);