iOS常用宏

2016-10-13  本文已影响13人  下雨之後
// 屏幕宽高
#define kScreenW    [UIScreen mainScreen].bounds.size.width
#define kScreenH    [UIScreen mainScreen].bounds.size.height
// 状态栏高
#define kStatusH    [UIApplication sharedApplication].statusBarFrame.size.height
// 导航栏高
#define kNavigationBarH self.navigationController.navigationBar.frame.size.height
// tabbar高
#define kTabBarH    self.tabBarController.tabBar.frame.size.height
// 获得RGB颜色
#define kColorRGB(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
// 获得RGB颜色自定义透明度
#define kColorRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:a]
// 颜色值宏函数
#define kColorFromRGB(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]
// 颜色值自定义透明度
#define kColorFromRGBA(rgbValue, a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:a]
// 判断是否为iPhone4
#define kIPhone4 ([UIScreen mainScreen].bounds.size.height == 480)
// 判断是否为iPhone5
#define kIPhone5 ([UIScreen mainScreen].bounds.size.height == 568)
// 判断是否为iPhone6
#define kIPhone6 ([UIScreen mainScreen].bounds.size.height == 667)
// 判断是否为IPhone6Plus
#define kIPhone6Plus ([UIScreen mainScreen].bounds.size.height == 736)
// 判断是哪个版本
#define kIOS(version) ([[UIDevice currentDevice].systemVersion doubleValue] >= version)
#define kIOS8 kIOS(8.0)
#define kIOS9 kIOS(9.0)
#define kIOS10 kIOS(10.0)
// appdelegate
#define kAppDelegate (AppDelegate *)[UIApplication sharedApplication].delegate
// 自定义Log
#ifdef DEBUG  // 调试阶段
#define MYLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else // 发布阶段
#define MYLog(...)
#endif
// 获取系统版本号
#define kSystemVersion [[[UIDevice currentDevice] systemVersion] floatValue]
单例宏
// 1. 解决.h文件
#define singletonInterface(className)          + (instancetype)shared##className;

// 2. 解决.m文件
// 判断 是否是 ARC
#if __has_feature(objc_arc)
#define singletonImplementation(className) \
static id instance; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = [super allocWithZone:zone]; \
}); \
return instance; \
} \
+ (instancetype)shared##className { \
    static dispatch_once_t onceToken; \
    dispatch_once(&onceToken, ^{ \
        instance = [[self alloc] init]; \
    }); \
    return instance; \
} \
- (id)copyWithZone:(NSZone *)zone { \
    return instance; \
}
#else
// MRC 部分
#define singletonImplementation(className) \
static id instance; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = [super allocWithZone:zone]; \
}); \
return instance; \
} \
+ (instancetype)shared##className { \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
instance = [[self alloc] init]; \
}); \
return instance; \
} \
- (id)copyWithZone:(NSZone *)zone { \
return instance; \
} \
- (oneway void)release {} \
- (instancetype)retain {return instance;} \
- (instancetype)autorelease {return instance;} \
- (NSUInteger)retainCount {return ULONG_MAX;}

#endif

// 提示,最后一行不要使用 \
上一篇下一篇

猜你喜欢

热点阅读