Skip to content

Commit

Permalink
Updated logic for scrolling of the segmented page
Browse files Browse the repository at this point in the history
  • Loading branch information
sladan-hedvig committed Aug 30, 2024
1 parent 00028bd commit 27b5677
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions Projects/Contracts/Sources/View/ScrollableSegmentedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ class ScrollableSegmentedViewModel: NSObject, ObservableObject {
scrollTo(offset: offsetToScrollTo)
}

func scrollToTab(withVelocity: CGFloat) {
let moveFor = abs(Int(withVelocity / 2)) + 1
if withVelocity < 0 {
if let current = pageModels.firstIndex(where: { $0.id == currentId }), current > 0 {
let scrollTo = max(0, current - moveFor)
if scrollTo >= 0 {
setSelectedTab(with: pageModels[scrollTo].id)
}
}
} else {
if let current = pageModels.firstIndex(where: { $0.id == currentId }), current < pageModels.count - 1 {
let scrollTo = min(pageModels.count - 1, current + moveFor)
if scrollTo < pageModels.count {
setSelectedTab(with: pageModels[scrollTo].id)
}
}
}
}

func setSelectedTab(with id: String) {
if let index = pageModels.firstIndex(where: { $0.id == id }) {
currentId = id
Expand All @@ -222,16 +241,27 @@ class ScrollableSegmentedViewModel: NSObject, ObservableObject {
}

extension ScrollableSegmentedViewModel: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollToNearestWith(offset: scrollView.contentOffset.x)
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate {
scrollToNearestWith(offset: scrollView.contentOffset.x)
}
}

func scrollViewWillEndDragging(
_ scrollView: UIScrollView,
withVelocity velocity: CGPoint,
targetContentOffset: UnsafeMutablePointer<CGPoint>
) {
if velocity.x != 0 {
if #available(iOS 17.4, *) {
UIView.animate(withDuration: 0.1) { [weak scrollView] in
scrollView?.stopScrollingAndZooming()
}
}
self.scrollToTab(withVelocity: velocity.x)
}
}

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
currentHeight = heights.values.max(by: { $1 > $0 }) ?? 0
}
Expand Down

0 comments on commit 27b5677

Please sign in to comment.