iOS 常用巨集定義大全

離歌回來了發表於2017-12-22
// Frame
#define SCREEN_FRAME    ([UIScreen mainScreen].applicationFrame)
// 寬度
#define SCREEN_WIDTH    ([UIScreen mainScreen].bounds.size.width)
// 高度
#define SCREEN_HEIGHT   ([UIScreen mainScreen].bounds.size.height)
// iPhone6寬比
#define KBaseW          [UIScreen mainScreen].bounds.size.width / 375
// iPhone6高比
#define KBaseH          [UIScreen mainScreen].bounds.size.height / 667
// RGBA
#define RGBA(R /*紅*/, G /*綠*/, B /*藍*/, A /*透明*/)   [UIColor colorWithRed: (float)R / 255.f green: (float)G / 255.f blue: (float)B / 255.f alpha: A]
// RGB
#define RGB(r, g, b)        [UIColor colorWithRed: (r) / 255.0 green: (g) / 255.0 blue: (b) / 255.0 alpha: 1.0]

// 沙盒目錄
#define kUserDefaults       [NSUserDefaults standardUserDefaults]

// 獲取通知中心
#define kNotificationCenter [NSNotificationCenter defaultCenter]

// 設定隨機顏色
#define kRandomColor        [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1.0]

// clear背景顏色
#define kClearColor         [UIColor clearColor]

// 自定義高效率的 NSLog
#ifdef DEBUG
  #define kLog(...) NSLog(@"%s 第%d行 \n %@\n\n", __func__, __LINE__, [NSString stringWithFormat:__VA_ARGS__])
#else
  #define kLog(...)

#endif

// 弱引用/強引用
#define kWeakSelf(type)     __weak typeof(type) weak##type = type;
#define kStrongSelf(type)   __strong typeof(type) type = weak##type;

// 設定 view 圓角和邊框
#define kViewBorderRadius(View, Radius, Width, Color) \
                                                      \
    [View.layer setCornerRadius: (Radius)];           \
    [View.layer setMasksToBounds:YES];                \
    [View.layer setBorderWidth:(Width)];              \
    [View.layer setBorderColor:[Color CGColor]]

// 由角度轉換弧度 由弧度轉換角度
#define kDegreesToRadian(x)         (M_PI * (x) / 180.0)
#define kRadianToDegrees(radian)    (radian * 180.0) / (M_PI)

// 獲取keyWindow
#define kWindow [UIApplication sharedApplication].keyWindow

// 設定狀態條載入
#define kShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
// 收起狀態條載入
#define kHideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO

// MBProgressHUD載入
#define kShowHUDAndActivity kBackView; [MBProgressHUD showHUDAddedTo:kWindow animated:YES]; kShowNetworkActivityIndicator()

// MBProgressHUD隱藏
#define kHiddenHUD          [MBProgressHUD hideAllHUDsForView:kWindow animated:YES]; kHideNetworkActivityIndicator()

// 獲取圖片資源
#define kGetImage(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@", imageName]]

// 獲取當前語言
#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

// 使用 ARC 和 MRC
#if __has_feature(objc_arc)
    // ARC
#else
    // MRC
#endif

// 判斷是否為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 5/5s/SE
#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))

// 判斷是真機還是模擬器
#if TARGET_OS_IPHONE
    // iPhone Device
#endif

#if TARGET_IPHONE_SIMULATOR
    // iPhone Simulator
#endif

// 沙盒目錄檔案
// 獲取temp
#define kPathTemp       NSTemporaryDirectory()

// 獲取沙盒 Document
#define kPathDocument   [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]

// 獲取沙盒 Cache
#define kPathCache      [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

// GCD 的巨集定義
// GCD - 一次性執行
#define kDISPATCH_ONCE_BLOCK(onceBlock)                     static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);

// GCD - 在Main執行緒上執行
#define kDISPATCH_MAIN_THREAD(mainQueueBlock)               dispatch_async(dispatch_get_main_queue(), mainQueueBlock);

// GCD - 開啟非同步執行緒
#define kDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock)    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);
複製程式碼

沒有一蹴而就的天才,只有不斷點亮天賦的勤者

相關文章