iOS中常用的宏定义总结
程序员文章站
2022-06-23 20:55:24
前言
宏定义在c系开发中可以说占有举足轻重的作用,为了简化开发流程,提升工作效率,收集了一些平时常用的宏定义,今后会不定期更新
1.ui元素
//navbar...
前言
宏定义在c系开发中可以说占有举足轻重的作用,为了简化开发流程,提升工作效率,收集了一些平时常用的宏定义,今后会不定期更新
1.ui元素
//navbar高度 #define navigationbar_height 44 //statusbar高度 #define statusbar_height 20 //获取屏幕 宽度、高度 #define screen_width ([uiscreen mainscreen].bounds.size.width) #define screen_height ([uiscreen mainscreen].bounds.size.height) //内容视图高度 #define content_height (screen_height - navigationbar_height - statusbar_height) //kwindow #define kwindow [uiapplication sharedapplication].keywindow //屏幕分辨率 #define screen_resolution (screen_width * screen_height * ([uiscreen mainscreen].scale)) //状态栏 + 导航栏 高度 #define status_and_navigation_height ((statusbar_height) + (navigationbar_height))
2.log
//(在系统log基础之上,加入自定义的相关信息) #define nslog(format, ...) do { \ fprintf(stderr, "<%s : %d> %s\n", \ [[[nsstring stringwithutf8string:__file__] lastpathcomponent] utf8string], \ __line__, __func__); \ (nslog)((format), ##__va_args__); \ fprintf(stderr, "-------\n"); \ } while (0)
3.系统
//获取当前系统版本 #define ios_version [[[uidevice currentdevice] systemversion] floatvalue] #define currentsystemversion [[uidevice currentdevice] systemversion] //获取当前系统语言 #define currentlanguage ([[nslocale preferredlanguages] objectatindex:0]) //判断是不是真机 #if target_os_iphone //iphone device #endif //判断是不是模拟器 #if target_iphone_simulator //iphone simulator #endif //是否在arc环境下 #if __has_feature(objc_arc) //compiling with arc #else //compiling without arc #endif //判断是否为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 5(s)(e) #define iphone5se [[uiscreen mainscreen] bounds].size.width == 320.0f &&[[uiscreen mainscreen] bounds].size.height == 568.0f //判断是否为iphone 6/6s #define iphone6_6s [[uiscreen mainscreen] bounds].size.width == 375.0f &&[[uiscreen mainscreen] bounds].size.height == 667.0f //判断是否为iphone 6plus/6splus #define iphone6plus_6splus [[uiscreen mainscreen] bounds].size.width == 414.0f && [[uiscreen mainscreen] bounds].size.height == 736.0f //判断 ios 或更高的系统版本 #define ios_version_6_or_later (([[[uidevice currentdevice] systemversion] floatvalue]>=6.0)? (yes):(no)) #define ios_version_7_or_later (([[[uidevice currentdevice] systemversion] floatvalue]>=7.0)? (yes):(no)) #define ios_version_8_or_later (([[[uidevice currentdevice] systemversion] floatvalue]>=8.0)? (yes):(no)) #define ios_version_9_or_later (([[[uidevice currentdevice] systemversion] floatvalue]>=9.0)? (yes):(no)) #define ios_version_10_or_later (([[[uidevice currentdevice] systemversion] floatvalue]>=10.0)? (yes):(no)) //系统版本工具 #define system_version_equal_to(v) ([[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] == nsorderedsame) #define system_version_greater_than(v) ([[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] == nsordereddescending) #define system_version_greater_than_or_equal_to(v) ([[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] != nsorderedascending) #define system_version_less_than(v) ([[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] == nsorderedascending) #define system_version_less_than_or_equal_to(v) ([[[uidevice currentdevice] systemversion] compare:v options:nsnumericsearch] != nsordereddescending) //检测是否是竖屏状态 #define isportrait ([uiapplication sharedapplication].statusbarorientation == uiinterfaceorientationportrait || [uiapplication sharedapplication].statusbarorientation == uiinterfaceorientationportraitupsidedown)
4.颜色类
//带有rgba的颜色设置 #define color(r, g, b, a) [uicolor colorwithred:r/255.0 green:g/255.0 blue:b/255.0 alpha:a] //设置随机颜色(调试时候很有用) #define randomcolor [uicolor colorwithred:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0] //16进制颜色 #define rgb16color(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]
5.通知
//获取通知中心 #define notificationcenter [nsnotificationcenter defaultcenter] //快速发通知 #define post_notify(_notificationname, _obj, _userinfodictionary) [[nsnotificationcenter defaultcenter] postnotificationname: _notificationname object: _obj userinfo: _userinfodictionary]; //添加观察者 #define add_observer(_notificationname, _observer, _observerselector, _obj) [[nsnotificationcenter defaultcenter] addobserver:_observer selector:@selector(_observerselector) name:_notificationname object: _obj]; //移除观察者 #define remove_observer(_observer) [[nsnotificationcenter defaultcenter] removeobserver: _observer];
6.数据存储
//nsuserdefaults 实例化 #define user_default [nsuserdefaults standarduserdefaults] //获取temp #define kpathtemp nstemporarydirectory() //获取沙盒document #define kpathdocument [nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask, yes) firstobject] //获取沙盒cache #define kpathcache [nssearchpathfordirectoriesindomains(nscachesdirectory,nsuserdomainmask, yes) firstobject]
7.单例模式
#define singleh(name) +(instancetype)share##name; #if __has_feature(objc_arc) //条件满足 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 //mrc #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;\ }\ -(oneway void)release\ {\ }\ \ -(instancetype)retain\ {\ return _instance;\ }\ \ -(nsuinteger)retaincount\ {\ return maxfloat;\ } #endif
8.时间
//获取系统时间戳 #define curenttime [nsstring stringwithformat:@"%ld", (long)[[nsdate date] timeintervalsince1970]]
9.权限
//获取相机权限状态 #define camerastatus [avcapturedevice authorizationstatusformediatype:avmediatypevideo] #define cameradenied ((camerastatus == avauthorizationstatusrestricted)||(camerastatus == avauthorizationstatusdenied)) #define cameraallowed (!cameradenyed) /** 定位权限*/ #define locationstatus [cllocationmanager authorizationstatus]; #define locationallowed ([cllocationmanager locationservicesenabled] && !((status == kclauthorizationstatusdenied) || (status == kclauthorizationstatusrestricted))) #define locationdenied (!locationallowed) /** 消息推送权限*/ #define pushclose (([[uidevice currentdevice].systemversion floatvalue]>=8.0f)?(uiusernotificationtypenone == [[uiapplication sharedapplication] currentusernotificationsettings].types):(uiremotenotificationtypenone == [[uiapplication sharedapplication] enabledremotenotificationtypes])) #define pushopen (!pushclose)
10.本地文件加载
#define loadimage(file,type) [uiimage imagewithcontentsoffile:[[nsbundle mainbundle]pathforresource:file oftype:type]] #define loadarray(file,type) [nsarray arraywithcontentsoffile:[[nsbundle mainbundle]pathforresource:file oftype:type]] #define loaddict(file,type) [nsdictionary dictionarywithcontentsoffile:[[nsbundle mainbundle]pathforresource:file oftype:type]]
11.block
//弱引用 #define weakwithnameandobject(obj,name) __weak typeof(obj) weak##name = obj //强引用 #define strongwithnameandobject(obj,name) __strong typeof(obj) strong##name = obj
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。
上一篇: ai怎么设计欧式花纹图案?
下一篇: ADB命令行控制界面开关