pch文件創(chuàng)建
-
在工程中新建一個(gè)PCH 文件:New File ->Other ->PCH File(這就是需要新建的pch文件,點(diǎn)擊該好名字和路徑) ->Create吼蚁。這時(shí)候就創(chuàng)建好了一個(gè)pch文件
-
配置pch路徑:
- Build Setting 設(shè)置中(搜索 Prefix Header)
找到Precomplie Prefix Header 這項(xiàng)設(shè)置設(shè)置成YES饰豺。 - 把Precomplie Prefix Header 下面一項(xiàng)Prefix Header 雙擊打開削咆,把pch文件拖到打開的對(duì)話框中贬循,回車轮听。
- 把(SRCROOT)拷貝代替 Prefix Header 中的路徑 替換前為:/Users/xxx/Desktop/WLZMakeTrip/WLZMakeTrip/PrefixHeader.pch 替換后為:(SRCROOT)/WLZMakeTrip/PrefixHeader.pch趁冈, 回車掉弛。
headerFile創(chuàng)建
-
在工程中新建一個(gè)headerFile 文件:New File ->Source -> Header File(這就是需要新建的headerFile文件,點(diǎn)擊該好名字和路徑) ->Create搅幅。這時(shí)候就創(chuàng)建好了一個(gè)headerFile文件
如果Precompile Prefix Header為YES咽斧,那么pch會(huì)被預(yù)編譯堪置,預(yù)編譯后的pch文件會(huì)被緩存起來躬存,從而提高編譯速度
如果Precompile Prefix Header為NO,那么pch不會(huì)被預(yù)編譯舀锨,而是在每一個(gè)用到它導(dǎo)入的框架類庫的.m文件中編譯一次
常用宏定義的整理
我們可以在PCH 文件進(jìn)行宏定義岭洲。headerFile文件中定義接口。并在PCH文件中導(dǎo)入headerFile文件就可全局使用宏定義和接口坎匿。
- 獲取屏幕寬度與高度
//需要橫屏或者豎屏盾剩,獲取屏幕寬度與高度
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 當(dāng)前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
- 獲取通知中心
#define skyNotificationCenter [NSNotificationCenter defaultCenter]
- 設(shè)置顏色
- 顏色
#define skyRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define skyRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]
- 十六進(jìn)制顏色
#define skyColorWithHex(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]
- 隨機(jī)顏色
#define skyRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
- clear背景顏色
#define skyClearColor [UIColor clearColor]
- 淺灰顏色
#define skyGrayColor kRGBColor(220, 220, 220)
- 背景色
#define skyBACKGROUNDCOLOR [UIColor colorWithRed:242.0/255.0 green:236.0/255.0 blue:231.0/255.0 alpha:1.0]
- 加載本地文件
- 圖片
#define skyLoadImage(file,type) [UIImageimageWithContentsOfFile:[[NSBundlemainBundle]pathForResource:fileofType:type]]
- 數(shù)組
#define skyLoadArray(file,type) [UIImagearrayWithContentsOfFile:[[NSBundlemainBundle]pathForResource:fileofType:type]]
- 字典
#define skyLoadDict(file,type) [UIImagedictionaryWithContentsOfFile:[[NSBundlemainBundle]pathForResource:fileofType:type]]
- 自定義高效率的 NSLog(開發(fā)的時(shí)候打印,但是發(fā)布的時(shí)候不打印的NSLog)
#ifdef DEBUG
#define SLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define SLog(...)
#endif
- 弱引用(block循環(huán)引用)
#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;
- 設(shè)置 view 圓角和邊框
#define skyViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]
- 由角度轉(zhuǎn)換弧度 由弧度轉(zhuǎn)換角度
#define skyDegreesToRadian(x) (M_PI * (x) / 180.0)
#define skyRadianToDegrees(radian) (radian*180.0)/(M_PI)
- 字符串是否為空
#define skyStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || ([str length] < 1) ? (YES) : (NO))
- 數(shù)組是否為空
#define skyArrayIsEmpty(array) (array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0)
- 獲取圖片資源
#define skyGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]
- 獲取當(dāng)前語言
#define skyCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
- 屏幕尺寸
// 狀態(tài)欄占用高度
#define skyStateBarHeight 20.f
// 狀態(tài)欄底部y坐標(biāo)
#define skyOffsetStateBarHeight ((DEVICE_OS_VERSION_VALUE >= 7.0)? StateBarHeight : 0.f)
// 頂部狀態(tài)欄占用高度
#define skyTopNavBarHeight 44.f
// 頂部導(dǎo)航欄底部y坐標(biāo)
#define skyOffsetTopNavBarHeight (OffsetStateBarHeight + TopNavBarHeight)
// 底部tabbar占用高度
#define skyBottomTabBarHeight 49.f
// 屏幕可操作高度
#define skyMainHeight ((DEVICE_OS_VERSION_VALUE >= 7.0)? SCREENH_HEIGHT : (SCREENH_HEIGHT - StateBarHeight))
// 屏幕可操作寬度
#define skyMainWidth SCREEN_WIDTH
// 去除上下導(dǎo)航欄剩余中間視圖高度
#define skyContentHeight (skyMainHeight -skyOffsetTopNavBarHeight -skyBottomTabBarHeight)
- 判斷當(dāng)前的iPhone設(shè)備/系統(tǒng)版本
- 判斷是否為iPhone
#define sky_IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
- 判斷是否為iPad
#define sky_IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
- 判斷是否為ipod
#define sky_IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
- 判斷是否為 iPhone 5SE
#define sky_iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f
- 判斷是否為iPhone 6/6s/7
#define sky_iPhone6_6s_7 [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f
- 判斷是否為iPhone 6Plus/6sPlus/7Plus
#define sky_iPhone6Plus_6sPlus_7Plus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f
- 獲取系統(tǒng)版本
//這個(gè)方法不是特別靠譜
#define sky_IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//建議使用這個(gè)方法
#define sky_IOS_SYSTEM_VERSION_STRING [[UIDevice currentDevice] systemVersion]
- 判斷 iOS 8 或更高的系統(tǒng)版本
#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))
- 判斷是真機(jī)還是模擬器
#if TARGET_OS_IPHONE
//iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
- 沙盒目錄文件
//NSUserDefaults 實(shí)例化
#define skyUserDefaults [NSUserDefaults standardUserDefaults]
//獲取temp
#define skyPathTemp NSTemporaryDirectory()
//獲取沙盒 Document
#define skyPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//獲取沙盒 Cache
#define skyPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
- GCD 的宏定義
- GCD - 一次性執(zhí)行
#define sky_DISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
- GCD - 在Main線程上運(yùn)行
#define sky_DISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
- GCD - 開啟異步線程
#define sky_DISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);