我正在使用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的方法,这样看起来半打不会发生任何事情绘制所需的秒数(当不在主线程中时)?
答案 0 :(得分: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