JTAppleCalendar icon indicating copy to clipboard operation
JTAppleCalendar copied to clipboard

Calling scrollToSegment consecutively stutters the calendar view

Open soorinpark opened this issue 8 years ago • 9 comments

IMPORTANT - BEFORE CREATING YOUR ISSUE PLEASE READ THE FOLLOWING:

  1. PLEASE STATE THE VERSION NUMBER you are using. 7.1
  2. Are you using the latest version of JTAppleCalendar? Latest version is currently 7.1.5 no
  3. 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

  1. wait for the calendar is done scrolling and disables scrolling till current scrolling is complete
  2. 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?

soorinpark avatar Jul 31 '18 16:07 soorinpark

does this help?

    @IBAction func next(_ sender: UIButton) {
        sender.isEnabled = false
        self.calendarView.scrollToSegment(.next) {
            sender.isEnabled = true
        }
    }

patchthecode avatar Aug 06 '18 16:08 patchthecode

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

soorinpark avatar Aug 13 '18 15:08 soorinpark

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 avatar Aug 13 '18 18:08 patchthecode

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

soorinpark avatar Aug 13 '18 18:08 soorinpark

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.

patchthecode avatar Aug 14 '18 00:08 patchthecode

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 avatar Mar 05 '19 13:03 yuvigr

@yuvigr what version of the cal are u using

patchthecode avatar Mar 05 '19 14:03 patchthecode

@patchthecode Hi, i'm using the 7.1.6 version

yuvigr avatar Mar 07 '19 13:03 yuvigr

@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

patchthecode avatar Mar 07 '19 19:03 patchthecode