美文网首页
iOS 常用宏定义

iOS 常用宏定义

作者: moxacist | 来源:发表于2017-08-28 16:27 被阅读13次

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

    • 颜色
    #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
    /** 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
    /** version */
    #define iOSVersion(version) ([[UIDevice currentDevice].systemVersion doubleValue] >= version)
    #define KSystemVersion [[UIDevice currentDevice] systemVersion]
    #define KAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
    
    • log
    #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
    
    • appdelegate 相关
    #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 => 希望能帮到大家

    相关文章

      网友评论

          本文标题: iOS 常用宏定义

          本文链接:https://www.haomeiwen.com/subject/cpdgdxtx.html