patchthecode / JTAppleCalendar

The Unofficial Apple iOS Swift Calendar View. Swift calendar Library. iOS calendar Control. 100% Customizable
https://patchthecode.com
MIT License
7.58k stars 810 forks source link

set start date after calendar load #206

Closed yumsmools closed 8 years ago

yumsmools commented 8 years ago

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

patchthecode commented 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.

?

yumsmools commented 8 years ago

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
    }
patchthecode commented 8 years ago

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.

yumsmools commented 8 years ago

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

patchthecode commented 8 years ago

Awesome. Glad I could help.

patchthecode commented 8 years ago

@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.

marktrobinson commented 5 years ago

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