【问题标题】:EKRecurrenceRule (xcode iOS calendar interaction - Phonegap Plugin): Help creating a fairly complex event patternEKRecurrenceRule(xcode iOS 日历交互 - Phonegap 插件):帮助创建相当复杂的事件模式
【发布时间】:2014-01-14 18:43:54
【问题描述】:

我已经成功地为 iPhone 修改了 Calendar.m 和 Calendar.h 插件来创建一个相当复杂的事件:

在给定的月份, 3周每7天有一个事件, 这个月的最后一周没有任何事情发生。

这一直有效,直到我意识到两件事: 1. 这是一个给定的月份从开始日期。开始日期可以是月份、中间、结束或开始的任何时间。 2. 并非所有月份都有 28 天。我必须考虑 29、30、31。 3. 结束日期和开始日期一样,不一定是月初或月底。

我的 EKRecurrenceRule 声明:

EKRecurrenceRule* rrule = [[EKRecurrenceRule alloc] initRecurrenceWithFrequency:EKRecurrenceFrequencyMonthly
interval:1
daysOfTheWeek:nil
daysOfTheMonth:validDays monthsOfTheYear:nil weeksOfTheYear:nil daysOfTheYear:nil setPositions:nil end:rend];

我的 validDays 数组(在上面的 daysOfTheMonth 中提供)

//Note: 22 is week 4. This will be excluded. 
validDays = [[NSArray alloc]initWithObjects:
[NSNumber numberWithInt:1],
[NSNumber numberWithInt:8],
[NSNumber numberWithInt:15],
[NSNumber numberWithInt:29], nil];

EKAlarm 顺便说一句,为方便起见,偏移量为 0。

是的,它现在是硬编码的。我知道我必须更改 validDays 数组,并且我知道如何获取当天的天数,但我不知道从这里去哪里。 EKRecurrenceRule 的文档似乎没有涵盖从月初开始到月底结束的并发症案例。

更改开始日期和结束日期,至少在 iOS7 中,似乎不会自动进行任何偏移。如果我从本月 10 日开始,f'rex,第一个提醒是在 15 号(8 号和 1 号已经过去)——当我想要 17 号的第一个提醒时。第一次提醒应该是从开始日期算起的 7 天,然后每次提醒都在 7 天后,每 3 周休息 1 周。

我目前的印象是,要考虑从月中开始和不等的月份长度,我可能必须创建多个 EKRecurrenceRules,并且通过扩展多个每周甚至每月的事件都不算作重复事件。这不是预期的结果,因为此类事件在日历中很难删除。

有什么帮助吗?

【问题讨论】:

  • 呃...好吧,我肯定需要一个答案。在循环中添加单独的日子需要血腥的年龄,并且不适合分发。 (一年的可变日期持续时间为 5 分钟,即使在 Calendar.m 而不是 Calendar.js 中完成。)
  • 没人?我猜 EKRecurrence 并没有我想象的那么发达。我将不得不得出结论,从月中开始考虑显然是不可行的。

标签: ios cordova calendar


【解决方案1】:

第三次在这里回答我自己的问题。

编辑:我意识到这个事件序列是针对一系列事件,从一个月中的任意位置开始,每月 21 天,直到结束日期。我稍后会调整代码以匹配问题,但此解决方案与回答问题有关,包括限制。

以下内容的有效期最长为 1 年。如果你决定使用它超过 1 年,祝你好运,我真的无法计算出类似的东西,尤其是考虑到闰年。

  NSMutableArray *validDays = [[NSMutableArray alloc] init];
  NSMutableArray *ignoreDays = [[NSMutableArray alloc] init];
   //according to this algorithm, I only have 28 days a month. Even if a month has 31 or       30 or 29 days. This is because the schedule is counted by WEEK.
  NSInteger currday = 1;

  //excludeWeek denotes the week of a given month that I'll exclude, eg: 1 is the first week, 4 is the last week. This allows me to exclude any week of a given 28-day cycle. 

  switch (excludeWeek)
  {

      case 1:
         for (NSInteger i = 1; i <= 7; ++i)
         {
            [ignoreDays addObject:[NSNumber numberWithInt:i]];
         }
       break;

       case 2:
         for (NSInteger i = 8; i <= 14; ++i)
         {
            [ignoreDays addObject:[NSNumber numberWithInt:i]];
         }

       break;

       case 3:
         for (NSInteger i = 15; i <= 21; ++i)
         {
             [ignoreDays addObject:[NSNumber numberWithInt:i]];
         }

         break;

       default: //or case 4:

         for (NSInteger i = 22; i <= 28; ++i)
         {
             [ignoreDays addObject:[NSNumber numberWithInt:i]];
         }
         break; 
     }

     //this line here is why I can only guarantee results of 1 year. Anything more and endDay may very well be 1 day after startday. I'm using day of year, the only possible values are 1-365 or -1 to -365, +-1 for leap years. 
     if (startDay == endDay) endDay -= 1;

     while (startDay != endDay)
     {

         ++startDay;

         if (isLeapYear)
         {
              //Feb has 1 more day remember?
              if (startDay > 366) startDay = 1;
         }
         else
         {
              if (startDay > 365) startDay = 1;
         }


         if ([ignoreDays containsObject:[NSNumber numberWithInteger:currday] ] == FALSE)

             [validDays addObject: [NSNumber numberWithInt:startDay]];

         ++currday;
         if (currday > 28) currday = 1;
   }

   EKRecurrenceRule* rrule = [[EKRecurrenceRule alloc]     initRecurrenceWithFrequency:EKRecurrenceFrequencyYearly
   interval:1
   daysOfTheWeek:nil
   daysOfTheMonth:nil 
   monthsOfTheYear:nil 
   weeksOfTheYear:nil 
   daysOfTheYear:[NSArray arrayWithArray:validDays] 
   setPositions:nil end:rend];

希望任何人都不需要持续超过一年的复杂序列。使用这种方法,您应该知道如何以同样的方式填充一年中的几周。

这仍然会触发请在单独的线程中运行警告,但它比手动执行要快得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多