美文网首页提高iOS的开发效率iOS开发坚持写
iOS开发常用宏定义,加快开发速度

iOS开发常用宏定义,加快开发速度

作者: GavinKang | 来源:发表于2016-08-25 21:52 被阅读1106次

系统高度

//屏幕的宽高
#define HitoScreenW [UIScreen mainScreen].bounds.size.width
#define HitoScreenH [UIScreen mainScreen].bounds.size.height
//屏幕大小
#define HitoScreenSize [UIScreen mainScreen].bounds
//比例宽和高(以6s为除数)
#define HitoActureHeight(height)  roundf(height/375.0 * HitoScreenW)
#define HitoActureWidth(Width)  roundf(Width/667.0 * HitoScreenH)
//状态栏的高度
#define HitoStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
//导航栏的高度
#define HitoNavBarHeight 44.0
//iphoneX-SafeArea的高度
#define HitoSafeAreaHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?34:0)
//分栏+iphoneX-SafeArea的高度
#define HitoTabBarHeight (49+HitoSafeAreaHeight)
//导航栏+状态栏的高度
#define HitoTopHeight (HitoStatusBarHeight + HitoNavBarHeight)

快速查询一段代码的执行时间

#define TOCK NSLog(@"Time:%f", -[[NSDate date] timeIntervalSinceNow]);

blcok 和self 引起的循环引用警告⚠️的处理问题

#define Custom_Weak(weakSelf)  __weak __typeof(&*self)weakSelf = self;

代码中加入NSLog的暴力调试方法是很频繁的,但是在release的时候要删除这些调试代码,那工作量是烦躁,这样的情况下,试用宏就会显得非常的方便

//非常实用的打印,包括打印的类名——方法名-行数-打印的内容
#ifdef DEBUG
#define LRLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
#else
#define LRLog(...)
#endif

获得主 window

#define Window [[UIApplication sharedApplication].delegate window]

自动退出的提示框,需要和获得的主 window 结合使用,同时导入第三方库 Toast

#define Toast(str)  CSToastStyle *style = [[CSToastStyle alloc] initWithDefaultStyle]; \
[Window  makeToast:str duration:0.6 position:CSToastPositionCenter style:style];\
Window.userInteractionEnabled = NO; \
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{\
Window.userInteractionEnabled = YES;\
});\

iPad Air {{0, 0}, {768, 1024}}

iPhone4s {{0, 0}, {320, 480}} 960640
iPhone5 5s {{0, 0}, {320, 568}} 1136
640
iPhone6 6s {{0, 0}, {375, 667}} 1334750
iPhone6Plus 6sPlus {{0, 0}, {414, 736}} 1920
1080
Apple Watch 1.65inches(英寸) 320*640

获取手机的 UUID以及屏幕的尺寸

#define UUID [[[UIDevice currentDevice] identifierForVendor] UUIDString]

#define APP_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define APP_HEIGHT ([UIScreen mainScreen].bounds.size.height)

#define TabBarH        49.0f
#define StatusBarH     20.0f
#define NavigationBarH 44.0f

// 根据 iPhone6 6s 的尺寸为例
#define WIDTH  APP_WIDTH/375
#define HEIGHT APP_HEIGHT/667

获取View的属性

#define Get_View_Width(Width)  view.frame.size.width
#define Get_View_Height(Height) view.frame.size.height
#define Get_View_X(X)      view.frame.origin.x
#define Get_View_Y(Y)      view.frame.origin.y

分辨设备类型

//判断是否 Retina屏、设备是否iPhone 5、是否是iPad
#define Is_Retina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)

/** 判断是否为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 4/4S 分辨率320x480,像素640x960,@2x */
#define iPhone4 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)

/** 设备是否为iPhone 5C/5/5S 分辨率320x568,像素640x1136,@2x */
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

/** 设备是否为iPhone 6 分辨率375x667,像素750x1334,@2x */
#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)

/** 设备是否为iPhone 6 Plus 分辨率414x736,像素1242x2208,@3x */
#define iPhone6P ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

设置View的圆角和边框

#define View_Border_Radius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

由角度转换弧度/由弧度转换角度

#define DegreesToRadian(x) (M_PI * (x) / 180.0)

#define RadianToDegrees(radian) (radian*180.0)/(M_PI)

获取图片

获取图片资源
#define GetImage(imageName) [UIImage imageNamed:imageName]
读取本地图片
#define Load_Image(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]]

获取文件路径

//获取temp
#define Path_Temp NSTemporaryDirectory()
//获取沙盒 Document
#define Path_Document [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//获取沙盒 Cache
#define Path_Cache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

GCD 的宏定义(需要自己加上^{})

//GCD - 一次性执行
#define DISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);
//GCD - 在Main线程上运行
#define DISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);
//GCD - 开启异步线程
#define DISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);

设置颜色

//设置随机颜色
#define Random_Color [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
设置颜色和透明度
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
// rgb颜色转换(16进制->10进制)
#define UIColorFromRGB(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]

//带有RGBA的颜色设置
#define Color_Alpha(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]

#define Color_Clear [UIColor clearColor]

#define COLOR_BLUE_             UIColorFromRGB(0x41CEF2)
#define COLOR_GRAY_             UIColorFromRGB(0xababab) //171
#define COLOR_333               UIColorFromRGB(0x333333) //51
#define COLOR_666               UIColorFromRGB(0x666666) //102
#define COLOR_888               UIColorFromRGB(0x888888) //136
#define COLOR_999               UIColorFromRGB(0x999999) //153
#define COLOR_PLACEHOLD_        UIColorFromRGB(0xc5c5c5) //197
#define COLOR_RED_              UIColorFromRGB(0xff5400) //红色
#define COLOR_GREEN_            UIColorFromRGB(0x31d8ab)//绿色
#define COLOR_YELLOW_           UIColorFromRGB(0xffa200)//黄色
#define COLOR_SEPARATE_LINE     UIColorFromRGB(0xC8C8C8)//200
#define COLOR_LIGHTGRAY         COLOR(200, 200, 200, 0.4)//淡灰色

判断操作系统版本

#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

/** 获取系统版本 */
#define iOS_VERSION ([[[UIDevice currentDevice] systemVersion] floatValue])
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])

/** 是否为iOS6 */
#define iOS6 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) ? YES : NO)

/** 是否为iOS7 */
#define iOS7 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) ? YES : NO)

/** 是否为iOS8 */
#define iOS8 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) ? YES : NO)

/** 是否为iOS9 */
#define iOS9 (([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0) ? YES : NO)

NSUserDefaults 存/获得/删对象

//存储对象

#define UserDefaultSetObjectForKey(__VALUE__,__KEY__) \
{\
[[NSUserDefaults standardUserDefaults] setObject:__VALUE__ forKey:__KEY__];\
[[NSUserDefaults standardUserDefaults] synchronize];\
}
 //获得存储的对象
#define UserDefaultObjectForKey(__KEY__)  [[NSUserDefaults standardUserDefaults] objectForKey:__KEY__]

//     删除对象
#define UserDefaultRemoveObjectForKey(__KEY__) \
{\
[[NSUserDefaults standardUserDefaults] removeObjectForKey:__KEY__];\
[[NSUserDefaults standardUserDefaults] synchronize];\
}

宏定义一个弹窗方法,括号里面是方法的参数

#define ShowAlert(message)    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:self cancelButtonTitle:@"cancel" otherButtonTitles: @"OK"];[alert show];

与后台连调的宏定义

//定义一个API
#define API_URL                @"http://www.google.com"
//login the API 登陆API
#define API_Login              [APIURL stringByAppendingString:@"Login"]

//根据不同的系统,设置不同的动画

#define YES_Or_NO    floorf([[UIDevice currentDevice].systemVersion floatValue]) >8.0 ? YES : NO

借鉴别人的,加上自己的一些经验,希望对大家有用,也希望大家补充

相关文章

网友评论

  • Rchongg:命名不规范啊
    GavinKang:@Rchongg 谢谢提醒,这是我借鉴的别人的+自己积累的,以后会注意的

本文标题:iOS开发常用宏定义,加快开发速度

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