iOS 开发中一些常用的宏

2016-12-12  本文已影响21人  小鱼儿喜欢花无缺

1.获取屏幕宽度与高度

```

#define SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width

#define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height

```

2.获取通知中心

```

#define JKNotificationCenter [NSNotificationCenter defaultCenter]

```

3.设置随机颜色

```

#define JKRandomColor  [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

```

4.设置RGB颜色/设置RGBA颜色

```

#define JKRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]

#define JKRGBAColor(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(r)/255.0 blue:(r)/255.0 alpha:a]

```

// clear背景颜色

```

#define JKClearColor [UIColor clearColor]

```

5.自定义高效率的 NSLog

```

#ifdef DEBUG

#define JKLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])

#else

#define JKLog(...)

#endif

```

6.弱引用/强引用

```

#define JKWeakSelf(type)  __weak typeof(type) weak##type = type;

#define JKStrongSelf(type)  __strong typeof(type) type = weak##type;

```

7.设置 view 圆角和边框

```

#define LRViewBorderRadius(View, Radius, Width, Color)\

\

[View.layer setCornerRadius:(Radius)];\

[View.layer setMasksToBounds:YES];\

[View.layer setBorderWidth:(Width)];\

[View.layer setBorderColor:[Color CGColor]]

```

8.GCD 的宏定义

```

//GCD - 一次性执行

#define kDISPATCH_ONCE_BLOCK(onceBlock) static dispatch_once_t onceToken; dispatch_once(&onceToken, onceBlock);

//GCD - 在Main线程上运行

#define kDISPATCH_MAIN_THREAD(mainQueueBlock) dispatch_async(dispatch_get_main_queue(), mainQueueBlock);

//GCD - 开启异步线程

#define kDISPATCH_GLOBAL_QUEUE_DEFAULT(globalQueueBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), globalQueueBlocl);

```

上一篇下一篇

猜你喜欢

热点阅读