iOS 常用宏定义

2017-08-28  本文已影响13人  moxacist

话不多说,以下是我在项目开发中经常用到的宏定义:

#define RGBColor(r, g, b)             [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]
#define RGBAlphaColor(r, g, b, a)     [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:(a)]
#define ColorFromRGB(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 SS_lineColor RGBColor(221 ,221, 221)
#define SS_appColor  RGBColor(221 ,221, 221)

其中十六进制转颜色的宏调用是:ColorFromRGB(0x333333)

/** frame */
#define kDEVICEWIDTH  [UIScreen mainScreen].bounds.size.width
#define kDEVICEHEIGHT  [UIScreen mainScreen].bounds.size.height
#define kScreen_Frame  (CGRectMake(0, 0 , kDEVICEWIDTH,kDEVICEHEIGHT))
#define RATE_HEIGHT  [UIScreen mainScreen].bounds.size.height/667
#define RATE_WIDTH  [UIScreen mainScreen].bounds.size.width/375
/** version */
#define iOSVersion(version) ([[UIDevice currentDevice].systemVersion doubleValue] >= version)
#define KSystemVersion [[UIDevice currentDevice] systemVersion]
#define KAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
#ifdef DEBUG
#define SSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define SSLog(...)
#endif
#define KPathTemp NSTemporaryDirectory() //获取temp
#define KPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] //获取沙盒 Document
#define KPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] //获取沙盒 Cache
#define kApplication        [UIApplication sharedApplication]
#define kKeyWindow          [UIApplication sharedApplication].keyWindow
#define kAppDelegate        [UIApplication sharedApplication].delegate
#define kUserDefaults      [NSUserDefaults standardUserDefaults]
#define KNav        [UIApplication sharedApplication].delegate.window.rootViewController;
#define singleH(name) +(instancetype)share##name;

#if __has_feature(objc_arc)

#define singleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)share##name\
{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}
#else
#define singleM static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+(instancetype)shareTools\
{\
return [[self alloc]init];\
}\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(oneway void)release\
{\
}\
\
-(instancetype)retain\
{\
return _instance;\
}\
\
-(NSUInteger)retainCount\
{\
return MAXFLOAT;\
}
#endif

#define ShareInstace(name) [name share##name]

这个有点长,具体调用方法是在h文件中singleH(TestClass)
m文件中singM(TestClass),调用单例的时候用ShareInstance(TestClass)

time over => 希望能帮到大家
上一篇 下一篇

猜你喜欢

热点阅读