Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ScrollingMode.stopAtEachFirstRowOfSection #1247

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion SampleJTAppleCalendar/Example Calendars/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class ViewController: UIViewController {
.nonStopToSection(withResistance: 0.5),
.stopAtEach(customInterval: 374),
.stopAtEachCalendarFrame,
.stopAtEachSection
.stopAtEachSection,
.stopAtFirstRowOfEachSection
]

@IBAction func changeScroll(_ sender: Any) {
Expand Down
2 changes: 2 additions & 0 deletions Sources/JTAppleCalendar/CalendarEnums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public enum ScrollingMode: Equatable {
case stopAtEachCalendarFrame
/// stopAtEachSection - non-continuous scrolling that will stop at each section
case stopAtEachSection
/// stopAtFirstRowOfEachSection - non-continous scrolling that will stop at first row of each section
case stopAtFirstRowOfEachSection
/// stopAtEach - non-continuous scrolling that will stop at each custom interval
case stopAtEach(customInterval: CGFloat)
/// nonStopToSection - continuous scrolling that will stop at a section
Expand Down
2 changes: 1 addition & 1 deletion Sources/JTAppleCalendar/JTACMonthView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ open class JTACMonthView: UICollectionView {
var decelerationRateMatchingScrollingMode: CGFloat {
switch scrollingMode {
case .stopAtEachCalendarFrame: return UIScrollView.DecelerationRate.fast.rawValue
case .stopAtEach, .stopAtEachSection: return UIScrollView.DecelerationRate.fast.rawValue
case .stopAtEach, .stopAtEachSection, .stopAtFirstRowOfEachSection: return UIScrollView.DecelerationRate.fast.rawValue
case .nonStopToSection, .nonStopToCell, .nonStopTo, .none: return UIScrollView.DecelerationRate.normal.rawValue
}
}
Expand Down
46 changes: 46 additions & 0 deletions Sources/JTAppleCalendar/JTACScrollViewDelegates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,52 @@ extension JTACMonthView: UIScrollViewDelegate {

let snapForward = midPoint - ((maxSnap - midPoint) * modifiedPercentage)

scrollDecision(currentScrollDirectionValue: translation,
previousScrollDirectionValue: lastMovedScrollDirection,
forward: {
if theCurrentContentOffset >= snapForward || directionVelocity > 0 {
setTargetContentOffset(endOfCurrentSectionOffset)
} else {
setTargetContentOffset(endOfPreviousSectionOffset)
}
},
backward: {
if theCurrentContentOffset <= snapForward || directionVelocity < 0 {
setTargetContentOffset(endOfPreviousSectionOffset)
} else {
setTargetContentOffset(endOfCurrentSectionOffset)
}
})
case .stopAtFirstRowOfEachSection:
let section = scrollDecision(currentScrollDirectionValue: translation,
previousScrollDirectionValue: lastMovedScrollDirection,
forward: { return theCurrentSection},
backward: { return theCurrentSection - 1},
fixed: { return theCurrentSection})

guard section >= 0, section < calendarLayout.endOfSectionOffsets.count else {setTargetContentOffset(0); return}

func monthHeaderSize(for section: Int) -> CGFloat {
let defaultValue = self.lastMonthSize["default"] ?? 0
guard let monthNumber = self.monthInfoFromSection(section)?.month,
let month = MonthsOfYear(rawValue: monthNumber - 1) else { return defaultValue }

return self.lastMonthSize[month] ?? defaultValue
}

let previousSection = theCurrentSection - 1 < 0 ? 0 : theCurrentSection - 1
let nextSection = section == self.numberOfSections - 1 ? section : section + 1
let endOfCurrentSectionOffset = calendarLayout.endOfSectionOffsets[theCurrentSection] + monthHeaderSize(for: nextSection)
let endOfPreviousSectionOffset = calendarLayout.endOfSectionOffsets[previousSection] + monthHeaderSize(for: theCurrentSection)

let midPoint = (endOfCurrentSectionOffset + endOfPreviousSectionOffset) / 2
let maxSnap = calendarLayout.endOfSectionOffsets[section]

let userPercentage: CGFloat = 20
let modifiedPercentage = CGFloat((100 - userPercentage) / 100.0)

let snapForward = midPoint - ((maxSnap - midPoint) * modifiedPercentage)

scrollDecision(currentScrollDirectionValue: translation,
previousScrollDirectionValue: lastMovedScrollDirection,
forward: {
Expand Down