Closed yumsmools closed 8 years ago
Hey. I have set start and end date to be 100 years. And performance is great on an old iPhone 4s. I test this library on old phones.
Can you tell me what you are doing in your
func calendar(_ calendar: JTAppleCalendarView, willDisplayCell cell: JTAppleDayCellView, date: Date, cellState: CellState) {
}
Your calendar should not lag.
?
yeah nothing too much
func calendar(_ calendar: JTAppleCalendarView, willDisplayCell cell: JTAppleDayCellView, date: Date, cellState: CellState) {
setGregorianMonth() // display month
setHijriMonth() // display hijri month
let myCell = cell as! CalendarCell
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd"
myCell.gregorianDay.text = cellState.text
myCell.setupHijriDay(date: date)
// Setup text color
myCell.configureTextColor(cellState: cellState) // simply set colors for each day displayed
}
Ok I see the first problem.
If you have a 6-row calendar, then you have 42 cells. This means that the code you have there will be called for every of the 42 cells that is on the screen.
You have instanced a DateFormatter
in there.
let formatter = DateFormatter()
DateFormatter()
instances is already known to be notoriously slow. And you are instancing it 42 times, plus every time you do a scroll you are also instancing it. Your formatter should not be initialized in there. Can you move it out of that function? Initialize it once (and only once if you can), and then cache it in some variable. Your performance will be increased by more than 90% guaranteed 👍 . For more information on DateFormatter we can google search NSDateFormatter performance
.
Also, can you tell me what is in your configureTextColor(cellState)
function? That is another place for potential lag issues. If you are setting colors in there on the fly like this
myCellView.backgroundColor = UIColor.blue
Then you are also inviting lag issues. Please cache your colors. Create your color instance once, and then re-use the variables. We do not want to be creating UIColor
instances every time you do a scroll.
Lastly, consider leaving a star ★ rating on this Github repository if you like it. It is greatly needed for this project :)
Let me know if your lags are fixed.
Hi there.
Ok, Fixed. I moved the formatter out and I also set it to not animate the scroll and it worked like a charm.
Thanks
Imran
Awesome. Glad I could help.
@yumsmools one last thing. You could have left the scroll animate. If the scrolling is still not smooth, then there is something wrong in the code somewhere again. But if you removed the scroll animation just because of preference, then no worries. The design is up to you.
cache your colors
These colours are cached already...
// Some convenience methods to create colors. These colors will be as calibrated as possible.
// These colors are cached.
open class var black: UIColor { get } // 0.0 white
Hi there
Thanks for a great control
I'm setting startdate and enddate 1 year either side of today. and school to today when the calendar appears, but the scroll performance is not too great. So I decided to make the start date today and set the proper start date after the calendar appears. Is there an easy way to do this?
Thanks
Imran