Calling scrollToSegment consecutively stutters the calendar view
IMPORTANT - BEFORE CREATING YOUR ISSUE PLEASE READ THE FOLLOWING:
- PLEASE STATE THE VERSION NUMBER you are using. 7.1
- Are you using the latest version of JTAppleCalendar? Latest version is currently 7.1.5 no
- PROVIDE ENOUGH INPORMATION SO I CAN RECREATE THE PROBLEM.
I have a two buttons that let you scroll to the previous and next month segments.
func scrollTo(_ segment: SegmentDestination) {
calendarView.scrollToSegment(segment)
}
However if the button is pressed consecutively in a row, the calendar does not either
- wait for the calendar is done scrolling and disables scrolling till current scrolling is complete
- cache the second scroll input and executes it after it's been complete
I've also tried setting a flag to indicate if the scrolling is complete:
func scrollTo(_ segment: SegmentDestination) {
if enableMonthSelectionButtons {
enableMonthSelectionButtons = false
calendarView.scrollToSegment(segment) {
enableMonthSelectionButtons = true
}
}
}
or setting the enableMonthSelectionButtons in:
func calendar(_ calendar: JTAppleCalendarView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
enableMonthSelectionButtons = true
}
but neither worked. Any suggestions?
does this help?
@IBAction func next(_ sender: UIButton) {
sender.isEnabled = false
self.calendarView.scrollToSegment(.next) {
sender.isEnabled = true
}
}
@patchthecode nope that doesn't work :( I've tried multiple variations of that including my own flag, disabling the calendarView itself, and more, but it seems like the problem lies in where the completion handler is being called even before technically the scrolling is not complete.
I've been messing with the source code to see where the problem is, and will definitely put up a PR if I can figure it out but I would appreciate your insight as well!
Ok. Maybe i missed something.
@IBAction func next(_ sender: UIButton) {
sender.isEnabled = false
self.calendarView.scrollToSegment(.next) {
sender.isEnabled = true
}
}
sender.isEnabled = false should have disabled the next button so that it cannot be tapped.
Is the completion handler not being called so that next button remains disabled?
@patchthecode Nope, the opposite. So when you first tap the "next month" button, the sender is set to sender.isEnabled = false. And then the self.calendarView.scrollToSegment(.next) happens.
Assuming the completion handler gets called when the scrollToSegment(.next) operation is complete (i.e. it has completely scrolled to the next month), sender should be disabled until the scrolling is fully complete.
However what happens is that the completion handler gets called prematurely (i.e. while the scrolling is still happening to go to the next month), the sender.isEnabled gets set to true so the user is basically able to tap the "next month" button continuously even though the scrolling for the previous operation has not completed, resulting in a very "stuttery" behavior.
I unfortunately can't post the video of this behavior from the app but if that doesn't make sense I will try to post an example project.
in that case i will have to debug, but I do not have the time to do so right now. Therefore, change your code to this:
@IBAction func next(_ sender: UIButton) {
self.calendarView.scrollToSegment(.next)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
sender.isEnabled = false
}
}
change the time from 0.2 to what ever you like. Pick a time that happens just after the scrolling animation ends. 0.3 maybe? not sure.
Having this issue also, adding the code snippet didn't work (even on 1 sec delay). It happens randomly, sometimes happened after the first time calling scrollToSegment
@yuvigr what version of the cal are u using
@patchthecode Hi, i'm using the 7.1.6 version
@yuvigr I tried this code in version 7.1.7 and it works.
@IBAction func next(_ sender: UIButton) {
sender.isEnabled = false
self.calendarView.scrollToSegment(.next) {
sender.isEnabled = true
}
}
Let me know