CrossApp 宏定义

宏名: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和row
unsigned 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的实现 一个参数是section
unsigned 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
}

作者:admin,如若转载,请注明出处:https://www.web176.com/crossapp/10155.html

(0)
打赏 支付宝 支付宝 微信 微信
adminadmin
上一篇 2023年2月21日
下一篇 2023年2月21日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注