栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

用c/c++混合编程方式为ios/android实现一个自绘日期选择控件(一C)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用c/c++混合编程方式为ios/android实现一个自绘日期选择控件(一C)

3) 月历操作函数:


void calendar_set_year_month(SCalendar* calendar,int year,int month)
{
   assert(calendar);
   //if(calendar->date.year != year  calendar->date.month != month)
   {
      calendar->date.year = year;
      calendar->date.month = month;
      //每个day设置为1号
      calendar->date.day = 1;

      //blf:
      //参考上面图示,dayBeginIdx获得的是某个月1号相对日期区块中的索引,例如本例中1号索引为6
      //而dayCount表示当前月的天数
      //这样通过偏移与长度,我们可以很容易进行某些重要操作
      //例如在碰撞检测某个cell是否被点中时,可以跳过没有日期的cell
      //在绘图时检测某个cell是否找范围之外,如果之外就用灰色现实等等
      //通过偏移量和count,进行范围判断
      calendar->dayBeginIdx = date_get_week(&calendar->date);
      calendar->dayCount = date_get_month_of_day(calendar->date.year, calendar->date.month);
   }

}

void calendar_get_year_month(SCalendar* calendar,int* year,int* month)
{
   assert(calendar);
   if(year)
      *year = calendar->date.year;
   if(month)
      *month = calendar->date.month;
}


void calendar_init(SCalendar* calendar,CGSize ownerSize,float yearMonthHeight,float weekHeight)
{
   assert(calendar && calendar);

   //memset(calendar, 0, sizeof(SCalendar));

   calendar->size = ownerSize;
   calendar->yearMonthSectionHeight = yearMonthHeight;
   calendar->weekSectionHegiht = weekHeight;
   //blf:daySectionHeight是计算出来的
   calendar->daySectionHeight = ownerSize.height - yearMonthHeight - weekHeight;
   //blf:错误检测,简单期间,全部使用assert在debug时候进行中断
   assert(calendar->daySectionHeight > 0);

   //blf:初始化时显示本地当前的年月日
   //date_get_now(&calendar->date);

   calendar_set_year_month(calendar, calendar->date.year, calendar->date.month);
}


void calendar_get_year_month_section_rect(const SCalendar* calendar,CGRect* rect)
{
   assert(rect);
   memset(rect,0,sizeof(CGRect));
   rect->size.width = calendar->size.width;
   rect->size.height = calendar->yearMonthSectionHeight;
}


void calendar_get_week_section_rect(const SCalendar* calendar,CGRect* rect)
{
   assert(rect);
   memset(rect,0,sizeof(CGRect));
   rect->origin.y = calendar->yearMonthSectionHeight;
   rect->size.width = calendar->size.width;
   rect->size.height = calendar->weekSectionHegiht;
}


void calendar_get_day_section_rect(const SCalendar* calendar,CGRect* rect)
{
   assert(calendar && rect);
   memset(rect,0,sizeof(CGRect));
   rect->origin.y = calendar->yearMonthSectionHeight + calendar->weekSectionHegiht;
   rect->size.width = calendar->size.width;
   rect->size.height = calendar->daySectionHeight;
}





void calendar_get_week_cell_rect(const SCalendar* calendar,CGRect* rect,int idx)
{
   assert(calendar && rect && idx >= 0 && idx < 7);
   //获取星期区块
   calendar_get_week_section_rect(calendar, rect);
   //计算出cell的宽度
   float cellWidth = rect->size.width / 7.0F;
   //计算出x偏移量
   rect->origin.x = cellWidth * idx;
   rect->size.width = cellWidth;
}



void calendar_get_day_cell_rect(const SCalendar* calendar,CGRect* rect,int rowIdx,int columIdx)
{
   assert(calendar && rect && rowIdx >= 0 && rowIdx < 6 && columIdx >= 0 && columIdx < 7 );
   float cellWidth = calendar->size.width / 7.0F;
   float cellHeight = calendar->daySectionHeight / 6.0F;
   rect->origin.x = cellWidth  * columIdx;
   rect->origin.y = cellHeight * rowIdx;
   rect->size.width  = cellWidth;
   rect->size.height = cellHeight;
}


void calendar_get_day_cell_rect_by_index(const SCalendar* calendar,CGRect* rect,int idx)
{
   assert(calendar && rect && idx >= 0 && idx < 42);
   // (/ 和 %)符号的应用,用于计算出行列索引号
   int rowIdx   = (idx / 7);
   int columIdx = (idx % 7);
   calendar_get_day_cell_rect(calendar, rect, rowIdx, columIdx);

}


int calendar_get_hitted_day_cell_index(const SCalendar* calendar, CGPoint localPt)
{
   //优化1: 如果一个点不在日期区块中,那么肯定没点中,立即返回
   CGRect daySec;
   calendar_get_day_section_rect(calendar, &daySec);

   if(!CGRectContainsPoint(daySec,localPt))
      return -1;

   localPt.y -= daySec.origin.y;

   //触摸点肯定会会点中日期区块中的某个cell

   //优化2: 避免使用循环6*7次遍历整个cell,检测是否一点在该cell中,通过下面算法,可以立刻获得当前点所在的cell行列索引号

   float cellWidth  =   daySec.size.width  / 7.0F;
   float cellHeight =   daySec.size.height / 6.0F;
   int   columIdx   =   localPt.x / cellWidth;
   int   rowIdx     =   localPt.y / cellHeight;

   //检测当前被点中的cell是否允许被选中,具体原理请参考
   //函数void calendar_set_year_month(SCalendar* calendar,int year,int month)的注释

   int idx  =  rowIdx * 7 + columIdx;
   if(idx < calendar->dayBeginIdx  idx > calendar->dayBeginIdx  + calendar->dayCount - 1)
      return -1;

   //到此说明肯定有点中的cell,返回该cell的索引号
   return idx;
}

第一部分完毕,下一篇我们关注ios的实现。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/233123.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号