当前位置:K88软件开发文章中心编程语言APP编程CrossApp → 文章内容

宏定义

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-23 13:52:51

由 Alma 创建, 最后一次修改 2017-10-10 宏名:CALLBACK_BIND_1宏定义:#define CALLBACK_BIND_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)解释:这是回调宏定义,__selector__代表回调方法, __target__代表目标对象, CALLBACK_BIND_0表示回调方法没有参数,CALLBACK_BIND_1表示回调方法有一个参数,以此类推示例代码:// CALLBACK_BIND_2对应的tableViewHeightForRowAtIndexPath方法有两个参数m_pTableView->onCellHeightAtIndexPath(CALLBACK_BIND_2(TableViewTest::tableViewHeightForRowAtIndexPath, this)); // tableViewHeightForRowAtIndexPath的实现 两个参数分别为section和rowunsigned int TableViewTest::tableViewHeightForRowAtIndexPath(unsigned int section, unsigned int row){ return 130;} // CALLBACK_BIND_1对应的tableViewHeightForHeaderInSection方法有一个参数m_pTableView->onHeightForHeaderInSection(CALLBACK_BIND_1(TableViewTest::tableViewHeightForHeaderInSection, this)); // tableViewHeightForHeaderInSection的实现 一个参数是sectionunsigned int TableViewTest::tableViewHeightForHeaderInSection(unsigned int section){ return 50;}宏名:CC_LISTENING_FUNCTION宏定义:#define CC_LISTENING_FUNCTION(FUNCTION, VARNAME)\protected: std::function<FUNCTION> m_ob##VARNAME{nullptr};\public: void on##VARNAME(const std::function<FUNCTION>& var){ m_ob##VARNAME = var; }解释:主要替代delegate,之前的需要先设置代理,然后实现代理中的方法,现在就可以直接实现方法,方便、简洁。FUNCTION代表回调的方法,VARNAME代表类中的方法。示例:CC_LISTENING_FUNCTION(unsigned int(unsigned int section), HeightForHeaderInSection);// 解释:HeightForHeaderInSection为类CATableView的方法名,用到时前面需要加on,unsigned int section为回调方法的参数,// unsigned int为回调方法的返回值类型。 // 用法m_pTableView->onHeightForHeaderInSection(CALLBACK_BIND_1(TableViewTest::tableViewHeightForHeaderInSection, this)); // 实现tableViewHeightForHeaderInSection回调方法unsigned int TableViewTest::tableViewHeightForHeaderInSection(unsigned int section){ return 50;}判断平台的宏:#define CC_PLATFORM_UNKNOWN      0    // 未知平台#define CC_PLATFORM_IOS                 1    // 苹果手机#define CC_PLATFORM_ANDROID         2    // 安卓手机#define CC_PLATFORM_WIN32             3    // Windows系统#define CC_PLATFORM_LINUX              5    // LINUX 系统#define CC_PLATFORM_BADA               6   // 三星智能手机操作系统#define CC_PLATFORM_MAC                8    // 苹果的Mac系统#define CC_PLATFORM_EMSCRIPTEN   10    // EMSCRIPTEN系统#define CC_PLATFORM_WINRT             12    // windows rt#define CC_PLATFORM_WP8                13    // Windows Phone 8系统CC_TARGET_PLATFORM用于来判断平台。下面来看一个通过判断平台打开网址的示例:void openUrl(const std::string &url){// 如果当前系统是MAC系统#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]]; // 如果当前系统是IOS系统#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithUTF8String:url.c_str()]]]; #endif}

宏定义