2012-12-07 73 views
0

我正在使用RubyMotion並使用CKCalendarView實現了一個日曆,並且我有以下代碼突出顯示發生事件的日子。繪圖時1.5s的 -是否有可能使後臺線程運行得更快?

下面的方法從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 

稱爲因爲它是,這個凍結爲0.5的UI。如果我將它移動到後臺線程中,繪製時間需要4或5倍,但不會凍結UI。

問:有沒有辦法讓後臺線程更高的優先級,還是有辦法逐步繪製和突出dateButtons使得它看起來並不像什麼也沒有發生,以半打秒發生的事情繪製(當不在主線程中)?

+0

idk ruby​​motion但你應該總是在主線程中進行繪製。在後臺做你的邏輯,然後在主線程中進行回調來完成那個繪圖 – mkral

+0

那麼,如果我在循環內調用繪圖的主線程,性能會如何呢? – silasjmatson

+0

你能解釋一下這裏發生了什麼事嗎?這些陣列有多大?如果你能解釋一下datebutton是什麼,以及有多少,我對它們都不熟悉。他們代表一個月內的日子嗎?我會稍微查看文檔 – mkral

回答

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 
+0

我不確定這是否是要走的路(我不知道這是什麼開銷是在一個循環中多次調用主線程),但它的工作原理。 – silasjmatson

相關問題