0

我正在使用 RubyMotion 并使用 CKCalendarView 实现了一个日历并且我有以下代码突出显示事件发生的日期。

从以下方法调用layoutSubviews

def calendarDidLayoutSubviews(calendar)
  _events_array = []
  if self.events
    self.events.reverse.each do |ev|
      mdy = ev.date.month_date_year
      _events_array <<  mdy unless _events_array.include?(mdy)
    end
  end

  Dispatch::Queue.main.async do
    today = NSDate.date.month_date_year

    if calendar.dateButtons && calendar.dateButtons.length > 0
      calendar.dateButtons.each do |db|
        db.backgroundColor = "f4f2ee".to_color
        if db.date && db.date >= calendar.minimumDate && db.date <= calendar.maximumDate
          if _events_array && _events_array.length > 0
            db.setTitleColor("c7c3bb".to_color, forState:UIControlStateNormal)
            db.backgroundColor = "f4f2ee".to_color
            if _events_array.include?(db.date.month_date_year)
              db.setTitleColor(UIColor.blackColor, forState:UIControlStateNormal)
              db.backgroundColor = "9ebf6c".to_color
              _events_array.delete(db.date.month_date_year)
            end
          end
        end
        if db.date && db.date.month_date_year == today
          if calendar.minimumDate <= db.date && calendar.maximumDate >= db.date
            db.setTitleColor(UIColor.blackColor, forState:UIControlStateNormal)
          else
            db.setTitleColor("f4f2ee".to_color, forState:UIControlStateNormal)
          end
        end
      end
    end
  end
end

事实上,这会在绘制时将 UI 冻结 0.5 - 1.5 秒。如果我将它移到后台线程中,绘制时间会增加 4 到 5 倍,但不会冻结 UI。

问题:有没有办法给后台线程更高的优先级,或者有办法逐步绘制和突出显示 dateButtons,这样在绘制所需的六秒内看起来没有任何事情发生(当不在主线程)?

4

1 回答 1

0

我发布的方法在主线程中被调用,所以我将它切换为在后台线程中调用,然后在我的循环中为所有绘图操作调用主线程。

def calendarDidLayoutSubviews(calendar)
   _events_array = []
  if self.events
    self.events.reverse.each do |ev|
      mdy = ev.date.month_date_year
      _events_array <<  mdy unless _events_array.include?(mdy)
    end
  end

  today = NSDate.date.month_date_year

  if calendar.dateButtons && calendar.dateButtons.length > 0
    calendar.dateButtons.each do |db|
      Dispatch::Queue.main.async do
        db.backgroundColor = "f4f2ee".to_color
      end
      if db.date && db.date >= calendar.minimumDate && db.date <= calendar.maximumDate
        if _events_array && _events_array.length > 0
          Dispatch::Queue.main.async do
            db.setTitleColor("c7c3bb".to_color, forState:UIControlStateNormal)
            db.backgroundColor = "f4f2ee".to_color
          end
          if _events_array.include?(db.date.month_date_year)
            Dispatch::Queue.main.async do
              db.setTitleColor(UIColor.blackColor, forState:UIControlStateNormal)
              db.backgroundColor = "9ebf6c".to_color
            end
            _events_array.delete(db.date.month_date_year)
          end
        end
      end
      if db.date && db.date.month_date_year == today
        if calendar.minimumDate <= db.date && calendar.maximumDate >= db.date
          Dispatch::Queue.main.async do
            db.setTitleColor(UIColor.blackColor, forState:UIControlStateNormal)
          end
        else
          Dispatch::Queue.main.async do
            db.setTitleColor("f4f2ee".to_color, forState:UIControlStateNormal)
          end
        end
      end
    end
  end
end
于 2012-12-08T01:33:33.980 回答