在做項目的時候,直接拿過來使用,可以大幅度提高開發速度。
下面是 個人總結的一些宏定義。如果大家有其他的常用的宏定義,歡迎添加。我會定期更新這個blog…..
話不多說,直接上乾貨
// 在宏的參數前加上一個#,宏的參數會自動轉換成c語言的字符串
#define MRKeyPath(objc,keyPath) @(((void)objc.keyPath, #keyPath))
//** 加載xib ***********************************************************************************
#define LoadNib(x) [[NSBundle mainBundle] loadNibNamed:@(x) owner:nil options:nil][0]
//** 沙盒路徑 ***********************************************************************************
#define PATH_OF_APP_HOME NSHomeDirectory()
#define PATH_OF_TEMP NSTemporaryDirectory()
#define PATH_OF_DOCUMENT [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
//** DEBUG LOG *********************************************************************************
#ifdef DEBUG
#define MRLog( s, ... ) NSLog( @"< %@:(%d) > %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define MRLog( s, ... )
#endif
//** 獲得當前的 年 月 日 時 分 秒 *****************************************************************************
#define CurrentSec [[NSCalendar currentCalendar] component:NSCalendarUnitSecond fromDate:[NSDate date]]
#define CurrentMin [[NSCalendar currentCalendar] component:NSCalendarUnitMinute fromDate:[NSDate date]]
#define CurrentHour [[NSCalendar currentCalendar] component:NSCalendarUnitHour fromDate:[NSDate date]]
#define CurrentDay [[NSCalendar currentCalendar] component:NSCalendarUnitDay fromDate:[NSDate date]]
#define CurrentMonth [[NSCalendar currentCalendar] component:NSCalendarUnitMonth fromDate:[NSDate date]]
#define CurrentYear [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]]
//** 角度轉換成弧度 ******************************************************************************************
#define ANGEL(x) (x)/180.0 * M_PI
//* Frame (宏 x, y, width, height)**************************************************************************
// App Frame
#define Application_Frame [[UIScreen mainScreen] applicationFrame]
// App Frame Height&Width
#define App_Frame_Height [[UIScreen mainScreen] applicationFrame].size.height
#define App_Frame_Width [[UIScreen mainScreen] applicationFrame].size.width
// MainScreen Height&Width
#define Main_Screen_Height [[UIScreen mainScreen] bounds].size.height
#define Main_Screen_Width [[UIScreen mainScreen] bounds].size.width
// View 座標(x,y)和寬高(width,height)
#define ViewxPos(v) (v).frame.origin.x
#define ViewyPos(v) (v).frame.origin.y
#define ViewWidth(v) (v).frame.size.width
#define ViewHeight(v) (v).frame.size.height
#define MinFrameX(v) CGRectGetMinX((v).frame)
#define MinFrameY(v) CGRectGetMinY((v).frame)
#define MidFrameX(v) CGRectGetMidX((v).frame)
#define MidFrameY(v) CGRectGetMidY((v).frame)
#define MaxFrameX(v) CGRectGetMaxX((v).frame)
#define MaxFrameY(v) CGRectGetMaxY((v).frame)
// 系統控件默認高度
#define kStatusBarHeight (20.f)
#define kTopBarHeight (44.f)
#define kBottomBarHeight (49.f)
#define kCellDefaultHeight (44.f)
#define kEnglishKeyboardHeight (216.f)
#define kChineseKeyboardHeight (252.f)
判斷不同版本執行不同方法的宏
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_0
ios7以上的
#else
// iPhone OS SDK 7.0 之前版本的處理
#endif