美文网首页
iOS 常用宏定义

iOS 常用宏定义

作者: 逆光少年 | 来源:发表于2016-12-24 16:58 被阅读43次
    1.获取屏幕宽度与高度(支持横屏)
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 // 当前Xcode支持iOS8及以上
    #define SCREEN_WIDTH ([[UIScreen mainScreen] respondsToSelector:@selector(nativeBounds)]?[UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale:[UIScreen mainScreen].bounds.size.width)
    #define SCREEN_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 SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
    #define SCREEN_SIZE [UIScreen mainScreen].bounds.size
    #endif
    
    2.设置RGB颜色/设置RGBA颜色
    #define RGB(r,g,b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0]
    #define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
    
    3.设置随机颜色
    #define LRRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
    
    4.获取通知中心
    #define XDNotificationCenter [NSNotificationCenter defaultCenter]
    
    5.弱引用/强引用
    #define XDWeakSelf(type) __weak typeof(type) weak##type = type;
    #define XDStrongSelf(type) __strong typeof(type) type = weak##type;
    
    6.获取沙盒目录文件
    //获取temp
    #define kPathTemp NSTemporaryDirectory()
    //获取沙盒 Document
    #define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
    //获取沙盒 Cache
    #define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
    
    7.自定义日志
    #ifdef DEBUG
    #define NSLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
    #else
    #define NSLog(...)
    
    #endif
    
    8.判断当前的iPhone设备/系统版本
    //判断是否为iPhone
    #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    #define IS_IPHONE ([[[UIDevice currentDevice] model] isEqualToString:@"iPhone"])
    
    //判断是否为iPad
    #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    #define IS_IPAD ([[[UIDevice currentDevice] model] isEqualToString:@"iPad"])
    
    //判断是否为ipod#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
    
    // 判断是否为 iPhone 5S/SE
    #define iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f && [[UIScreen mainScreen] bounds].size.height == 568.0f
    
    // 判断是否为iPhone 6/6s/7
    #define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f && [[UIScreen mainScreen] bounds].size.height == 667.0f
    
    // 判断是否为iPhone 6Plus/6sPlus/7p
    #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]
    
    //建议使用这个方法
    #define IOS_SYSTEM_STRING [[UIDevice currentDevice] systemVersion]
    
    //判断 iOS 8 或更高的系统版本
    #define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))
    
    9.判断是真机还是模拟器
    // 判断是不是iOS系统,如果是iOS系统在真机和模拟器输出都是YES
    #if TARGET_OS_IPHONE #endif 
    
    #if (TARGET_IPHONE_SIMULATOR)  
    // 在模拟器的情况下
    #else
     // 在真机情况下
    #endif
    

    相关文章

      网友评论

          本文标题:iOS 常用宏定义

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